Код: Выделить всё
__device__ void GetNetworkOutput(float* __restrict__ rollingdata, Network* net) {
Network net_copy;
for (int i = 0; i < net->num_neurons; ++i) {
net_copy.Neurons[i] = net->Neurons[i];
}
for (int i = 0; i < net->num_connections; ++i) {
net_copy.Connections[i] = net->Connections[i];
}
net_copy.Neurons[5].id = 31;
}
__global__ void EvaluateNetworks(float* __restrict__ rollingdata, Network* d_networks, int pop_num, int input_num, int output_num) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx >= pop_num) return;
Network* net = &d_networks[idx];
if (net->Neurons == nullptr || net->Connections == nullptr) {
printf("Network memory not allocated for index %d\n", idx);
return;
}
GetNetworkOutput(rollingdata, net);
printf("Original Neuron ID after GetNetworkOutput call: %i\n", net->Neurons[5].id);
}
Однако этот код работает довольно хорошо
Код: Выделить всё
__device__ void GetNetworkOutput(float* __restrict__ rollingdata, Network* net) {
__shared__ Neuron neurons_copy[1000];
__shared__ Connection connections_copy[1000];
for (int i = 0; i < net->num_neurons; ++i) {
neurons_copy[i] = net->Neurons[i];
}
for (int i = 0; i < net->num_connections; ++i) {
connections_copy[i] = net->Connections[i];
}
neurons_copy[5].id = 31;
}
__global__ void EvaluateNetworks(float* __restrict__ rollingdata, Network* d_networks, int pop_num, int input_num, int output_num) {
int idx = threadIdx.x + blockIdx.x * blockDim.x;
if (idx >= pop_num) return;
Network* net = &d_networks[idx];
if (net->Neurons == nullptr || net->Connections == nullptr) {
printf("Network memory not allocated for index %d\n", idx);
return;
}
GetNetworkOutput(rollingdata, net);
printf("Original Neuron ID after GetNetworkOutput call: %i\n", net->Neurons[5].id);
}
Structs;
struct Connection {
int innovationid;
int from;
int to;
float weight;
int type; // 0 input to hidden, 1 input to output, 2 hidden to hidden, 3 hidden to output
};
struct Neuron {
int type; // 0 input, 1 hidden, 2 output
float input_sum;
float bias;
float output;
int* incoming_connections;
int id;
int connected_num;
};
struct Network {
Connection* Connections;
Neuron* Neurons;
int num_neurons;
int num_connections;
float fitness;
};
< /code>
попробовал < /p>
Код: Выделить всё
__device__ void GetNetworkOutput(float* __restrict__ rollingdata, Network* net) {
Network net_copy;
net_copy.Neurons = (Neuron*)malloc(net->num_neurons * sizeof(Neuron));
if (net_copy.Neurons == NULL) {
printf("Neuron memory allocation failed.\n");
return;
}
net_copy.Connections = (Connection*)malloc(net->num_connections * sizeof(Connection));
if (net_copy.Connections == NULL) {
printf("Connection memory allocation failed.\n");
free(net_copy.Neurons);
return;
}
for (int i = 0; i < net->num_neurons; ++i) {
net_copy.Neurons[i] = net->Neurons[i];
}
for (int i = 0; i < net->num_connections; ++i) {
net_copy.Connections[i] = net->Connections[i];
}
net_copy.Neurons[5].id = 31;
free(net_copy.Neurons);
free(net_copy.Connections);
}
< /code>
и вычисления не удались. Также попробовал cudamalloc Как я могу это глубоко копировать?
Подробнее здесь: https://stackoverflow.com/questions/795 ... g-a-struct
Мобильная версия