wrote equivalent python code

This commit is contained in:
Robin 2024-12-26 17:34:41 +01:00
parent 3c8e68ac1d
commit 274622157a
2 changed files with 37 additions and 9 deletions

View File

@ -1,4 +1,4 @@
// #include "hurricanedata/fielddata.h"
#include "hurricanedata/fielddata.h"
#include "hurricanedata/gpubufferhandler.h"
#include "hurricanedata/datareader.h"
#include "hurricanedata/gpubuffer.h"
@ -10,9 +10,10 @@
#include <memory>
#include <iomanip>
__global__ void getSingleValue(float *ans, const FieldMetadata &fmd, FieldData fd) {
float xi = getVal(fmd, fd, 1, 20, 100, 100);
*ans = xi;
__global__ void middleOfTwoValues(float *ans, const FieldMetadata &fmd, FieldData fd) {
float xi = getVal(fmd, fd, 0, 20, 100, 100);
float yi = getVal(fmd, fd, 1, 20, 100, 100);
*ans = (xi+yi)/2;
}
int main() {
@ -20,7 +21,6 @@ int main() {
std::string variable = "T";
// std::unique_ptr<DataReader> dataReader = std::make_unique<DataReader>(path, variable);
DataReader dataReader{path, variable};
std::cout << "created datareader\n";
@ -38,16 +38,14 @@ int main() {
for (int i = 0; i < 10; i++) {
FieldData fd = bufferHandler.nextFieldData();
getSingleValue<<<1, 1>>>(ptr_test_read, *bufferHandler.fmd, fd);
middleOfTwoValues<<<1, 1>>>(ptr_test_read, *bufferHandler.fmd, fd);
cudaDeviceSynchronize();
std::cout << "ptr_test_read = " << std::fixed << std::setprecision(6) << *ptr_test_read << "\n";
}
// TODO: Write an example loop using buffering and measure it.
// TODO: Free data properly in FieldData (maybe make an iterator)
// TODO: measure data transfer time in this example code.
cudaFree(ptr_test_read);
return 0;
}

30
test_read3.py Normal file
View File

@ -0,0 +1,30 @@
import numpy as np
from netCDF4 import Dataset
# file_path = 'data/MERRA2_400.inst6_3d_ana_Np.20120101.nc4'
# ncfile = Dataset(file_path, 'r')
file_paths = [
'data/atmosphere_MERRA-wind-speed[179253532]/MERRA2_400.inst6_3d_ana_Np.20120101.nc4',
'data/atmosphere_MERRA-wind-speed[179253532]/MERRA2_400.inst6_3d_ana_Np.20120102.nc4',
'data/atmosphere_MERRA-wind-speed[179253532]/MERRA2_400.inst6_3d_ana_Np.20120103.nc4'
]
ncfiles = [Dataset(file_path) for file_path in file_paths]
# print(f"{Temp[0, 20, 100, 100]=}")
for i in range(10):
Temp = ncfiles[i//4].variables['T'][:]
x = Temp[i%4, 20, 100, 100]
Temp2 = ncfiles[(i+1)//4].variables['T'][:]
y = Temp2[(i+1)%4, 20, 100, 100]
print(f"{(x+y)/2=}")
# Close the NetCDF file
for ncfile in ncfiles:
ncfile.close()