mirror of
https://github.com/MartinOpat/cuda-based-raytrace.git
synced 2025-06-07 02:13:10 +02:00
load to gpu seems to work but the values are different than from python?
This commit is contained in:
36
src/main.cu
36
src/main.cu
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user