Skip to content

Commit 17a302e

Browse files
committed
Most changes to get EnvironmentManager refactor working.
3 tests inside SubEnvironmentManagerTest failing. Not checked python tests yet, some may have broken due to API change.
1 parent c9ca300 commit 17a302e

28 files changed

+290
-363
lines changed

include/flamegpu/exception/FLAMEGPUDeviceException_device.cuh

+13-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <cstring>
88

9+
#include "flamegpu/runtime/detail/SharedBlock.h"
10+
911
namespace flamegpu {
1012
namespace exception {
1113

@@ -81,20 +83,20 @@ class DeviceException {
8183
*/
8284
template<typename... Args>
8385
__device__ void setMessage(const char *format, Args... args) {
84-
extern __shared__ DeviceExceptionBuffer* buff[];
86+
using detail::sm;
8587
// Only the thread which first reported error gets to output
8688
if (hasError) {
8789
// Only output once
88-
if (buff[2]->format_string[0])
90+
if (sm()->device_exception->format_string[0])
8991
return;
9092
// Copy the format string
9193
unsigned int eos = 0;
9294
for (eos = 0; eos < DeviceExceptionBuffer::FORMAT_BUFF_LEN; ++eos)
9395
if (format[eos] == '\0')
9496
break;
95-
memcpy(buff[2]->format_string, format, eos * sizeof(char));
97+
memcpy(sm()->device_exception->format_string, format, eos * sizeof(char));
9698
// Process args
97-
subformat_recurse(buff[2], args...);
99+
subformat_recurse(sm()->device_exception, args...);
98100
}
99101
}
100102

@@ -135,18 +137,18 @@ class DeviceException {
135137
*/
136138
__device__ DeviceException(const char *file, const unsigned int line)
137139
: hasError(!getErrorCount()) {
138-
extern __shared__ DeviceExceptionBuffer* buff[];
140+
using detail::sm;
139141
if (hasError) {
140142
// Copy file location
141143
const size_t file_len = strlen(file);
142-
memcpy(buff[2]->file_path, file, file_len);
144+
memcpy(sm()->device_exception->file_path, file, file_len);
143145
// Copy line no
144-
buff[2]->line_no = line;
146+
sm()->device_exception->line_no = line;
145147
// Copy block/thread indices
146148
const uint3 bid3 = blockIdx;
147-
memcpy(buff[2]->block_id, &bid3, sizeof(unsigned int) * 3);
149+
memcpy(sm()->device_exception->block_id, &bid3, sizeof(unsigned int) * 3);
148150
const uint3 tid3 = threadIdx;
149-
memcpy(buff[2]->thread_id, &tid3, sizeof(unsigned int) * 3);
151+
memcpy(sm()->device_exception->thread_id, &tid3, sizeof(unsigned int) * 3);
150152
}
151153
}
152154
/**
@@ -188,9 +190,9 @@ __device__ inline void DeviceException::subformat(DeviceExceptionBuffer *buff, c
188190
}
189191
}
190192
__device__ unsigned int DeviceException::getErrorCount() {
191-
extern __shared__ DeviceExceptionBuffer* buff[];
193+
using detail::sm;
192194
// Are we the first exception
193-
return atomicInc(&buff[2]->error_count, UINT_MAX);
195+
return atomicInc(&sm()->device_exception->error_count, UINT_MAX);
194196
}
195197
#endif
196198
#else

include/flamegpu/io/JSONStateReader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class JSONStateReader : public StateReader {
3333
JSONStateReader(
3434
const std::string &model_name,
3535
const std::unordered_map<std::string, EnvironmentDescription::PropData> &env_desc,
36-
util::StringUint32PairUnorderedMap<util::Any> &env_init,
36+
std::unordered_map<std::string, util::Any>&env_init,
3737
util::StringPairUnorderedMap<std::shared_ptr<AgentVector>> &model_state,
3838
const std::string &input_file,
3939
Simulation *sim_instance);

include/flamegpu/io/StateReader.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class StateReader {
3737
StateReader(
3838
const std::string& _model_name,
3939
const std::unordered_map<std::string, EnvironmentDescription::PropData>& _env_desc,
40-
util::StringUint32PairUnorderedMap<util::Any>& _env_init,
40+
std::unordered_map<std::string, util::Any>& _env_init,
4141
util::StringPairUnorderedMap<std::shared_ptr<AgentVector>>& _model_state,
4242
const std::string& input,
4343
Simulation* _sim_instance)
@@ -67,7 +67,7 @@ class StateReader {
6767
std::string inputFile;
6868
const std::string model_name;
6969
const std::unordered_map<std::string, EnvironmentDescription::PropData> &env_desc;
70-
util::StringUint32PairUnorderedMap<util::Any> &env_init;
70+
std::unordered_map<std::string, util::Any>& env_init;
7171
Simulation *sim_instance;
7272
};
7373
} // namespace io

include/flamegpu/io/StateReaderFactory.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class StateReaderFactory {
3939
static StateReader* createReader(
4040
const std::string& model_name,
4141
const std::unordered_map<std::string, EnvironmentDescription::PropData>& env_desc,
42-
util::StringUint32PairUnorderedMap<util::Any>& env_init,
42+
std::unordered_map<std::string, util::Any>& env_init,
4343
util::StringPairUnorderedMap<std::shared_ptr<AgentVector>>& model_state,
4444
const std::string& input,
4545
Simulation* sim_instance) {

include/flamegpu/io/XMLStateReader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class XMLStateReader : public StateReader {
3232
XMLStateReader(
3333
const std::string &model_name,
3434
const std::unordered_map<std::string, EnvironmentDescription::PropData> &env_desc,
35-
util::StringUint32PairUnorderedMap<util::Any> &env_init,
35+
std::unordered_map<std::string, util::Any> &env_init,
3636
util::StringPairUnorderedMap<std::shared_ptr<AgentVector>> &model_state,
3737
const std::string &input_file,
3838
Simulation *sim_instance);

include/flamegpu/runtime/AgentFunction.cuh

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cuda_runtime.h>
55
#include <curand_kernel.h>
66

7+
#include "flamegpu/runtime/detail/SharedBlock.h"
78
#include "flamegpu/defines.h"
89
#include "flamegpu/exception/FLAMEGPUDeviceException.cuh"
910
#include "flamegpu/runtime/AgentFunction_shim.cuh"
@@ -18,7 +19,7 @@ typedef void(AgentFunctionWrapper)(
1819
exception::DeviceExceptionBuffer *error_buffer,
1920
#endif
2021
#ifndef __CUDACC_RTC__
21-
const detail::curve::Curve::CurveTable *d_curve_table,
22+
const detail::curve::CurveTable *d_curve_table,
2223
const char* d_env_buffer,
2324
#endif
2425
id_t *d_agent_output_nextID,
@@ -54,7 +55,7 @@ __global__ void agent_function_wrapper(
5455
exception::DeviceExceptionBuffer *error_buffer,
5556
#endif
5657
#ifndef __CUDACC_RTC__
57-
const detail::curve::Curve::CurveTable* d_curve_table,
58+
const detail::curve::CurveTable* d_curve_table,
5859
const char* d_env_buffer,
5960
#endif
6061
id_t *d_agent_output_nextID,
@@ -67,13 +68,13 @@ __global__ void agent_function_wrapper(
6768
unsigned int *scanFlag_agentOutput) {
6869
// We place these at the start of shared memory, so we can locate it anywhere in device code without a reference
6970
if (threadIdx.x == 0) {
70-
extern __shared__ const void* sm[];
71+
using detail::sm;
7172
#ifndef __CUDACC_RTC__
72-
sm[0] = d_curve_table;
73-
sm[1] = d_env_buffer;
73+
sm()->curve = d_curve_table;
74+
sm()->env_buffer = d_env_buffer;
7475
#endif
7576
#if !defined(SEATBELTS) || SEATBELTS
76-
sm[2] = error_buffer;
77+
sm()->device_exception = error_buffer;
7778
#endif
7879
}
7980

include/flamegpu/runtime/AgentFunctionCondition.cuh

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ typedef void(AgentFunctionConditionWrapper)(
1414
exception::DeviceExceptionBuffer *error_buffer,
1515
#endif
1616
#ifndef __CUDACC_RTC__
17-
const detail::curve::Curve::CurveTable* d_curve_table,
17+
const detail::curve::CurveTable* d_curve_table,
1818
const char* d_env_buffer,
1919
#endif
2020
const unsigned int popNo,
@@ -39,21 +39,21 @@ __global__ void agent_function_condition_wrapper(
3939
exception::DeviceExceptionBuffer *error_buffer,
4040
#endif
4141
#ifndef __CUDACC_RTC__
42-
const detail::curve::Curve::CurveTable* d_curve_table,
42+
const detail::curve::CurveTable* d_curve_table,
4343
const char* d_env_buffer,
4444
#endif
4545
const unsigned int popNo,
4646
curandState *d_rng,
4747
unsigned int *scanFlag_conditionResult) {
4848
// We place these at the start of shared memory, so we can locate it anywhere in device code without a reference
4949
if (threadIdx.x == 0) {
50-
extern __shared__ const void* sm[];
50+
using detail::sm;
5151
#ifndef __CUDACC_RTC__
52-
sm[0] = d_curve_table;
53-
sm[1] = d_env_buffer;
52+
sm()->curve = d_curve_table;
53+
sm()->env_buffer = d_env_buffer;
5454
#endif
5555
#if !defined(SEATBELTS) || SEATBELTS
56-
sm[2] = error_buffer;
56+
sm()->device_exception = error_buffer;
5757
#endif
5858
}
5959

include/flamegpu/runtime/DeviceAPI.cuh

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ReadOnlyDeviceAPI {
4444
exception::DeviceExceptionBuffer *,
4545
#endif
4646
#ifndef __CUDACC_RTC__
47-
const detail::curve::Curve::CurveTable *,
47+
const detail::curve::CurveTable *,
4848
#endif
4949
const unsigned int,
5050
curandState *,
@@ -148,7 +148,7 @@ class DeviceAPI {
148148
exception::DeviceExceptionBuffer *,
149149
#endif
150150
#ifndef __CUDACC_RTC__
151-
const detail::curve::Curve::CurveTable *,
151+
const detail::curve::CurveTable *,
152152
#endif
153153
id_t*,
154154
const unsigned int,
@@ -387,6 +387,7 @@ __device__ T ReadOnlyDeviceAPI::getVariable(const char(&variable_name)[M], const
387387
template<typename MessageIn, typename MessageOut>
388388
template<typename T, unsigned int N>
389389
__device__ T DeviceAPI<MessageIn, MessageOut>::getVariable(const char(&variable_name)[N]) const {
390+
using detail::sm;
390391
// simple indexing assumes index is the thread number (this may change later)
391392
const unsigned int index = (blockDim.x * blockIdx.x) + threadIdx.x;
392393

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef INCLUDE_FLAMEGPU_RUNTIME_DETAIL_SHAREDBLOCK_H_
2+
#define INCLUDE_FLAMEGPU_RUNTIME_DETAIL_SHAREDBLOCK_H_
3+
4+
namespace flamegpu {
5+
namespace exception {
6+
struct DeviceExceptionBuffer;
7+
} // namespace exception
8+
namespace detail {
9+
namespace curve {
10+
struct CurveTable;
11+
} // namespace curve
12+
/**
13+
* This struct represents the data we package into shared memory
14+
* The ifndef __CUDACC_RTC__ will cause the size to be too large for RTC builds, but that's not (currently) an issue
15+
*/
16+
struct SharedBlock {
17+
#ifndef __CUDACC_RTC__
18+
const curve::CurveTable* curve;
19+
const char* env_buffer;
20+
#endif
21+
#if !defined(SEATBELTS) || SEATBELTS
22+
exception::DeviceExceptionBuffer *device_exception;
23+
#endif
24+
};
25+
/**
26+
* Returns a pointer to a common item in shared memory
27+
*/
28+
__forceinline__ __device__ SharedBlock *sm() {
29+
__shared__ SharedBlock _sm;
30+
return &_sm;
31+
}
32+
} // namespace detail
33+
} // namespace flamegpu
34+
35+
#endif // INCLUDE_FLAMEGPU_RUNTIME_DETAIL_SHAREDBLOCK_H_

include/flamegpu/runtime/detail/curve/Curve.cuh

+10-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ class Curve {
2929
typedef unsigned int NamespaceHash; // !< Typedef for cuRVE variable namespace string hash
3030
static const int MAX_VARIABLES = 1024; // !< Default maximum number of cuRVE variables (must be a power of 2)
3131
static const VariableHash EMPTY_FLAG = 0;
32-
struct CurveTable {
33-
VariableHash hashes[MAX_VARIABLES]; // Device array of the hash values of registered variables
34-
char* variables[MAX_VARIABLES]; // Device array of pointer to device memory addresses for variable storage
35-
unsigned int type_size[MAX_VARIABLES]; // Device array of the types of registered variables
36-
unsigned int elements[MAX_VARIABLES];
37-
unsigned int count[MAX_VARIABLES];
38-
};
3932
/**
4033
* Main cuRVE variable hashing function
4134
*
@@ -61,6 +54,16 @@ class Curve {
6154
__host__ static VariableHash variableRuntimeHash(unsigned int num);
6255
#endif
6356
};
57+
/**
58+
* Representation of Curve's hashtable
59+
*/
60+
struct CurveTable {
61+
Curve::VariableHash hashes[Curve::MAX_VARIABLES]; // Device array of the hash values of registered variables
62+
char* variables[Curve::MAX_VARIABLES]; // Device array of pointer to device memory addresses for variable storage
63+
unsigned int type_size[Curve::MAX_VARIABLES]; // Device array of the types of registered variables
64+
unsigned int elements[Curve::MAX_VARIABLES];
65+
unsigned int count[Curve::MAX_VARIABLES];
66+
};
6467

6568
/* TEMPLATE HASHING FUNCTIONS */
6669

0 commit comments

Comments
 (0)