Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
gpu_arrays_functions.h
1#ifndef GPU_ARRAY_FUNCTIONS_H
2#define GPU_ARRAY_FUNCTIONS_H
3
4#include <iostream>
5
6#include "../global/global_cuda.h"
7#include "../utils/error_handling.h"
8#include "../utils/gpu.hpp"
9#include "../utils/gpu_arrays_functions.h"
10
11template <typename T>
12void Extend_GPU_Array(T **current_array_d, int current_size, int new_size, bool print_out)
13{
14 if (new_size <= current_size) {
15 return;
16 }
17 if (print_out) {
18 std::cout << " Extending GPU Array, size: " << current_size << " new_size: " << new_size << std::endl;
19 }
20
21 size_t global_free, global_total;
22 GPU_Error_Check(cudaMemGetInfo(&global_free, &global_total));
23 cudaDeviceSynchronize();
24#ifdef PRINT_GPU_MEMORY
25 printf("ReAllocating GPU Memory: %ld MB free \n", global_free / 1000000);
26#endif
27
28 if (global_free < new_size * sizeof(T)) {
29 printf("ERROR: Not enough global device memory \n");
30 printf(" Available Memory: %ld MB \n", global_free / 1000000);
31 printf(" Requested Memory: %ld MB \n", new_size * sizeof(T) / 1000000);
32 exit(-1);
33 }
34
35 T *new_array_d;
36 GPU_Error_Check(cudaMalloc((void **)&new_array_d, new_size * sizeof(T)));
37 cudaDeviceSynchronize();
38 GPU_Error_Check();
39 if (new_array_d == NULL) {
40 std::cout << " Error When Allocating New GPU Array" << std::endl;
41 chexit(-1);
42 }
43
44 // Copy the content of the original array to the new array
45 GPU_Error_Check(cudaMemcpy(new_array_d, *current_array_d, current_size * sizeof(T), cudaMemcpyDeviceToDevice));
46 cudaDeviceSynchronize();
47 GPU_Error_Check();
48
49 // Free the original array
50 cudaFree(*current_array_d);
51 cudaDeviceSynchronize();
52 GPU_Error_Check();
53
54 // Replace the pointer of the original array with the new one
55 *current_array_d = new_array_d;
56}
57
58#endif