CUDA получает ошибку памяти после глубокого копирования структурыC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 CUDA получает ошибку памяти после глубокого копирования структуры

Сообщение Anonymous »

Я пытаюсь работать с глубокопированными данными температуры, но когда я их реализую, он начинает давать ошибки памяти. Код, который я пытаюсь; < /p>

Код: Выделить всё

__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);
}

он ничего не печатает и дает неопределенную сбой запуска at cudadevicessynchronize
Однако этот код работает довольно хорошо

Код: Выделить всё

__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);
}

Но на этот раз он использует много ненужной памяти, и мы не можем использовать динамическое распределение, например, __shared__ Neuron Neurons_copy [net-> num_neurons]
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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»