still need to modify datareader to use netcdf on only one thread

This commit is contained in:
Robin
2024-12-23 20:48:00 +01:00
parent a7ad248c38
commit 7b3c87c656
11 changed files with 487 additions and 77 deletions

View File

@@ -1,19 +1,35 @@
#include "hurricanedata/fielddata.h"
// #include "hurricanedata/fielddata.h"
// #include "hurricanedata/gpubufferhandler.h"
#include "hurricanedata/datareader.h"
#include "hurricanedata/gpubuffer.h"
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <iostream>
#include <cmath>
#include <memory>
#include <iomanip>
// Not parallel computation
__global__ void computeMean(float *ans, const FieldMetadata &fmd, FieldData fd) {
// __global__ void computeMean(float *ans, const FieldMetadata &fmd, FieldData fd) {
// float sum = 0;
// size_t num_not_masked_values = 0;
// for (int i = 0; i < fmd.widthSize; i++) {
// double xi = getVal(fmd, fd, 2, 20, 100, i);
// if (xi < 1E14) { /* If x is not missing value */
// num_not_masked_values++;
// sum += xi;
// }
// }
// *ans = sum/num_not_masked_values;
// }
__global__ void computeMean(float *ans, DataHandle dh) {
float sum = 0;
size_t num_not_masked_values = 0;
for (int i = 0; i < fmd.widthSize; i++) {
double xi = getVal(fmd, fd, 2, 20, 100, i);
for (int i = 0; i < dh.size; i++) {
double xi = dh.d_data[i];
if (xi < 1E14) { /* If x is not missing value */
num_not_masked_values++;
sum += xi;
@@ -23,22 +39,37 @@ __global__ void computeMean(float *ans, const FieldMetadata &fmd, FieldData fd)
}
int main() {
std::string path = "data/MERRA2_400.inst6_3d_ana_Np.20120101.nc4";
std::string path = "data";
std::string variable = "T";
GPUBuffer buffer{path, variable};
auto fd = buffer.nextFieldData();
// std::unique_ptr<DataReader> dataReader = std::make_unique<DataReader>(path, variable);
DataReader dataReader{path, variable};
float *ptr_mean;
cudaMallocManaged(&ptr_mean, sizeof(float));
std::cout << "created datareader\n";
computeMean<<<1, 1>>>(ptr_mean, *buffer.fmd, fd);
GPUBuffer buffer (dataReader);
cudaDeviceSynchronize();
std::cout << "created buffer\n";
std::cout << "Mean = " << std::fixed << std::setprecision(6) << *ptr_mean << "\n";
auto dataHandle = buffer.getDataHandle(0);
cudaFree(fd.valArrays[0]);
cudaFree(ptr_mean);
// std::cout << "got a data handle\n";
// GPUBufferHandler buffer{path, variable};
// auto fd = buffer.nextFieldData();
// float *ptr_mean;
// cudaMallocManaged(&ptr_mean, sizeof(float));
// computeMean<<<1, 1>>>(ptr_mean, dataHandle);
// cudaDeviceSynchronize();
// std::cout << "Mean = " << std::fixed << std::setprecision(6) << *ptr_mean << "\n";
// // cudaFree(fd.valArrays[0]);
// cudaFree(ptr_mean);
return 0;
}