possibly loaded 3d array to device?

This commit is contained in:
Robin 2024-12-19 20:18:24 +01:00
parent 1c2ab4af76
commit 2719b93fa6
6 changed files with 34 additions and 11 deletions

4
.gitignore vendored
View File

@ -1,3 +1,3 @@
build build/
.vscode .vscode
data data/

Binary file not shown.

Binary file not shown.

View File

@ -23,3 +23,32 @@ std::vector<float> readData(std::string path, std::string variableName) {
return vec; return vec;
} }
struct cudaArray* 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
};
struct cudaArray *array;
cudaError_t error = cudaMalloc3DArray(&array, &arrayType, extent, 0);
cout << "cuda error: " << error << "\n";
return array;
}

View File

@ -5,5 +5,6 @@
#include <string> #include <string>
std::vector<float> readData(std::string path, std::string variableName); std::vector<float> readData(std::string path, std::string variableName);
struct cudaArray* loadDataToDevice(std::string path, std::string variableName);
#endif //DATAREADER_H #endif //DATAREADER_H

View File

@ -8,15 +8,8 @@
int main() { int main() {
std::string path = "data/MERRA2_400.inst6_3d_ana_Np.20120101.nc4"; std::string path = "data/MERRA2_400.inst6_3d_ana_Np.20120101.nc4";
std::string variable = "U"; std::string variable = "U";
auto x = readData(path, variable); auto arr = loadDataToDevice(path, variable);
cudaFreeArray(arr);
// Print some values from the file to see that it worked
int num = 0;
for(int i = 0; i < x.size(); i++) {
if (x[i] < 1E14) std::cout << x[i] << "\n";
if(num > 10000) break;
num++;
}
return 0; return 0;
} }