I am trying to find the minimum of an array which is on the gpu. I can use min_element on things on the cpu, but not sure how to do it on things on the gpu. Also I am confused why the return of min_element has to be an array since there's only one minimum? this is the closest to what I think is correct, but I get : ' error: no suitable conversion function from "thrust::device_ptr" to "double *" exists ' for the min_element line.
code:
#include <stdio.h> #include <stdlib.h> /* for rand() */ #include <unistd.h> /* for getpid() */ #include <time.h> /* for time() */ #include <math.h> #include <assert.h> #include <iostream> #include <ctime> #include <thrust/scan.h> #include <thrust/device_ptr.h> #include <thrust/reduce.h> #include <thrust/extrema.h> #include <cuda.h> using namespace std; bool errorAsk(const char *s="n/a") { cudaError_t err=cudaGetLastError(); if(err==cudaSuccess) return false; printf("CUDA error [%s]: %s\n",s,cudaGetErrorString(err)); return true; }; double *fillArray(double *c_idata,int N,double constant) { int n; for (n = 0; n < N; n++) { c_idata[n] = constant*floor(drand48()*10); } return c_idata; } int main(int argc,char *argv[]) { int N; N = 100; double *c_data,*g_data,*result; result = new double[N]; c_data = new double[N]; c_data = fillArray(c_data,N,1); cudaMalloc(&g_data,N*sizeof(double)); cudaMemcpy(g_data,c_data,N*sizeof(double),cudaMemcpyHostToDevice); thrust::device_ptr<double> g_ptr = thrust::device_pointer_cast(g_data); result = thrust::min_element(g_ptr, g_ptr + N); // not sure how to get this to work // result = thrust::max_element(c_data, c_data + N); //works but I need to do this on the gpu cudaMemcpy(c_data,g_data,N*sizeof(double),cudaMemcpyDeviceToHost); cout<<result[0]<<endl; }