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

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

View File

@@ -5,11 +5,43 @@
#include <iostream>
#include <cmath>
// Not parallel computation
__global__ void computeMean(float *ans, size_t *masked_vals, size_t n, float *x) {
float sum = 0;
size_t num_not_masked_values = 0;
size_t num_masked_values = 0;
for (int i = 0; i < n; i++) {
if (x[i] < 1E14) { /* If x is not missing value */
num_not_masked_values++;
sum += x[i];
} else {
num_masked_values++;
}
}
*ans = sum/num_not_masked_values;
*masked_vals = num_masked_values;
}
int main() {
std::string path = "data/MERRA2_400.inst6_3d_ana_Np.20120101.nc4";
std::string variable = "U";
std::string variable = "T";
auto arr = loadDataToDevice(path, variable);
cudaFreeArray(arr);
float *ptr_mean;
cudaMallocManaged(&ptr_mean, sizeof(float));
size_t *ptr_masked;
cudaMallocManaged(&ptr_masked, sizeof(size_t));
computeMean<<<1, 1>>>(ptr_mean, ptr_masked, arr.second, arr.first);
cudaDeviceSynchronize();
std::cout << "Mean = " << *ptr_mean << " calculated from " << arr.second << " values where " << *ptr_masked << " are masked values.\n";
cudaFree(arr.first);
cudaFree(ptr_mean);
cudaFree(ptr_masked);
return 0;
}