load to gpu seems to work but the values are different than from python?

This commit is contained in:
Robin committed 2024-12-20 12:24:44 +01:00
1 parent 2719b93fa6
commit ac63abe93a
5 files changed
+74 -23

No files matched your search

+24 -18
View File
@@ -24,31 +24,37 @@ std::vector<float> readData(std::string path, std::string variableName) {
return vec;
}
struct cudaArray* loadDataToDevice(std::string path, std::string variableName) {
std::pair<float*, size_t> loadDataToDevice(std::string path, std::string variableName) {
netCDF::NcFile data(path, netCDF::NcFile::read);
multimap<string, NcVar> vars = data.getVars();
NcVar var = vars.find(variableName)->second;
struct cudaChannelFormatDesc arrayType = {
.x = 32,
.y = 0,
.z = 0,
.w = 0,
.f = cudaChannelFormatKindFloat
}; // Float-32
struct cudaExtent extent = {
.width = var.getDim(3).getSize(), // longitude
.height = var.getDim(2).getSize(), // latitude
.depth = var.getDim(1).getSize(), // level
};
int length = 1;
for (NcDim dim: var.getDims()) {
length *= dim.getSize();
}
struct cudaArray *array;
// Store NetCDF variable in pinned memory on host
float *h_array;
cudaError_t error = cudaMalloc3DArray(&array, &arrayType, extent, 0);
cout << "cuda error: " << error << "\n";
cudaMallocHost(&h_array, sizeof(float)*length);
return array;
var.getVar(h_array);
// Copy data to device
float *d_array;
cudaError_t status = cudaMalloc(&d_array, sizeof(float)*length);
if (status != cudaSuccess)
cout << "Error allocating memory: " << status << "\n";
cudaMemcpyAsync(d_array, h_array, sizeof(float)*length, cudaMemcpyHostToDevice);
cudaDeviceSynchronize(); // Heavy hammer synchronisation // TODO: Use streams
cudaFreeHost(h_array);
return std::pair(d_array, length);
}
+1 -1
View File
@@ -5,6 +5,6 @@
#include <string>
std::vector<float> readData(std::string path, std::string variableName);
struct cudaArray* loadDataToDevice(std::string path, std::string variableName);
std::pair<float*, size_t> loadDataToDevice(std::string path, std::string variableName);
#endif //DATAREADER_H
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.