I am trying to cast a raw pointer using thrust::raw_pointer_cast to catch the output in the functor. I have tried multiple approaches to passing a pointer to a float, but keep getting a memory conflict and two intellisense errors thrust::system::cuda::thrust has no member "system_error" and has no member "cuda_category". The odd thing is that it seems to be an error in the program throw_on_error.hpp, which appears to be part of the BULK library even though I have not specifically referenced BULK. I'm new to C++ so it may be possible that I am misunderstanding pointers, or that I'm missing some sort of include.
Below is the version of the code I've been trying to get to work. Any help would be greatly appreciated.
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
#include <thrust/for_each.h>
#include <thrust/sequence.h>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cstdio>
#include <thread>
#include <thrust/copy.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/reduce.h>
using namespace std;
const int num_segs = 1; // number of segments to sort
const int num_vals = 5; // number of values in each segment
template <typename T>
struct sort_vector
{
T *Ndata;
T *Ddata;
T *answer;
sort_vector(T *_Ndata, T *_Ddata, float *a) : Ndata(_Ndata), Ddata(_Ddata), answer(a) {};
__host__ __device__ void operator()(int idx)
{
thrust::sort(thrust::seq, Ndata + idx*num_vals, Ndata + ((idx + 1)*num_vals));
thrust::sort(thrust::seq, Ddata + idx*num_vals, Ddata + ((idx + 1)*num_vals));
*answer = thrust::reduce(thrust::device, Ddata + idx*num_vals, Ddata + ((idx + 1)*num_vals));
}
};
int main() {
thrust::device_vector<float> d_Ndata(num_segs*num_vals);
d_Ndata[0] = 30;
d_Ndata[1] = 5.5;
d_Ndata[2] = 60;
d_Ndata[3] = 21;
d_Ndata[4] = 2;
thrust::device_vector<float> d_Ddata(num_segs*num_vals);
d_Ddata[0] = 50;
d_Ddata[1] = 9.5;
d_Ddata[2] = 30;
d_Ddata[3] = 8.1;
d_Ddata[4] = 1;
cout << "original norm" << endl;
int f = 0;
while (f < num_segs*num_vals){
cout << d_Ndata[f] << endl;
f++;
}
cout << "original dut" << endl;
int g = 0;
while (g < num_segs*num_vals){
cout << d_Ddata[g] << endl;
g++;
}
thrust::device_vector<int> d_idxs(num_segs);
thrust::sequence(d_idxs.begin(), d_idxs.end());
float *answer = (float*)malloc(sizeof(float));
cudaStream_t s1;
cudaStreamCreate(&s1);
clock_t start;
double duration;
start = clock();
thrust::for_each(thrust::cuda::par.on(s1),
d_idxs.begin(),
d_idxs.end(), sort_vector<float>(thrust::raw_pointer_cast(d_Ndata.data()), thrust::raw_pointer_cast(d_Ddata.data()), thrust::raw_pointer_cast(answer)));
cudaStreamSynchronize(s1);
cout << "sum" << endl;
cout << answer << endl;
//free(answer);
cudaStreamDestroy(s1);
duration = (clock() - start) / (double)CLOCKS_PER_SEC;
cout << "time " << duration << endl;
cin.get();
return 0;
}