Hi, I am writing a CUDA-MPI hybrid program and the external CUDA function does not work. I have a structure call our to host information, and inside this struct I have a variable call our_number_of_people. After I set this number to 50, I call cudamalloc, and the 50 get reset to 0. Why is cudamalloc touching host memory? I wrapped two print statement outside of the cudamalloc code and it returns different value…
void cuda_init(struct global_t *global, struct our_t *our,
struct stats_t *stats, struct cuda_t *cuda)
{
cuda->our_size = sizeof(int) * our->our_number_of_people;
cuda->their_size = sizeof(int) * global->total_number_of_people;
cuda->our_states_size = sizeof(char) * our->our_number_of_people;
// cuda memory allocation
cudaMalloc((void**)&cuda->their_infected_x_locations_dev, cuda->their_size);
cudaMalloc((void**)&cuda->their_infected_y_locations_dev, cuda->their_size);
printf("Place 1 is %d \n", our->our_number_of_people);
HANDLE_ERROR( cudaMalloc((void**)&cuda->our_x_locations_dev, cuda->our_size) );
printf("Place 2 is %d \n", our->our_number_of_people);
HANDLE_ERROR( cudaMalloc((void**)&cuda->our_y_locations_dev, cuda->our_size) );
HANDLE_ERROR( cudaMalloc((void**)&cuda->our_states_dev, cuda->our_states_size) );
HANDLE_ERROR( cudaMalloc((void**)&cuda->our_num_days_infected_dev, cuda->our_size) );
cudaMalloc((void**)&cuda->our_num_susceptible_dev, sizeof(int));
cudaMalloc((void**)&cuda->our_num_immune_dev, sizeof(int));
cudaMalloc((void**)&cuda->our_num_dead_dev, sizeof(int));
cudaMalloc((void**)&cuda->our_num_infected_dev, sizeof(int));
cudaMalloc((void**)&cuda->our_num_infections_dev, sizeof(int));
cudaMalloc((void**)&cuda->our_num_infection_attempts_dev, sizeof(int));
cudaMalloc((void**)&cuda->our_num_deaths_dev, sizeof(int));
cudaMalloc((void**)&cuda->our_num_recovery_attempts_dev, sizeof(int));
cuda->our_num_infections_int = (int)stats->our_num_infections;
cuda->our_num_infection_attempts_int = (int)stats->our_num_infection_attempts;
cuda->our_num_deaths_int = (int)stats->our_num_deaths;
cuda->our_num_recovery_attempts_int = (int)stats->our_num_recovery_attempts;
cudaMemcpy(cuda->their_infected_x_locations_dev, global->their_infected_x_locations, cuda->their_size, cudaMemcpyHostToDevice);
HANDLE_ERROR( cudaMemcpy(cuda->their_infected_y_locations_dev, global->their_infected_y_locations, cuda->their_size, cudaMemcpyHostToDevice) );
HANDLE_ERROR( cudaMemcpy(cuda->our_x_locations_dev, our->our_x_locations, cuda->our_size, cudaMemcpyHostToDevice) );
cudaMemcpy(cuda->our_y_locations_dev, our->our_y_locations, cuda->our_size, cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_states_dev, our->our_states, cuda->our_states_size, cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_days_infected_dev, our->our_num_days_infected, cuda->our_size, cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_susceptible_dev, &our->our_num_susceptible, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_immune_dev, &our->our_num_immune, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_dead_dev, &our->our_num_dead, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_infected_dev, &our->our_num_infected, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_infections_dev, &cuda->our_num_infections_int, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_infection_attempts_dev, &cuda->our_num_infection_attempts_int, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_deaths_dev, &cuda->our_num_deaths_int, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(cuda->our_num_recovery_attempts_dev, &cuda->our_num_recovery_attempts_int, sizeof(int), cudaMemcpyHostToDevice);
// set up 1D array for cuda
cuda->numThread = 128;
int tempBlock = (our->our_number_of_people+cuda->numThread-1)/cuda->numThread;
cuda->numBlock = (32 < tempBlock ? 32 : tempBlock);
// set up cuda Random Number Generator
cudaMalloc(&cuda->cuda_states, cuda->numThread * cuda->numBlock);
time_t current_time;
time(¤t_time);
rand_kernel<<<cuda->numBlock, cuda->numThread>>>(cuda->cuda_states,
(unsigned long)current_time);
}