Hello, everyone!
I am trying to find a way to use the STL library in CUDA and i found that i can use the thrust library as STL in C++ programming. So i am trying to use the ‘vector’ to CUDA but i had a lot of errors with programming. Because i am not well in English, i read a document of Thrust but cannot understand very well. Following code is my code for checking the thrust library but i failed several times. Please help me.
#include <iostream>
#include <cstdlib>
#include <chrono>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <device_functions.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
typedef struct point {
int x;
int y;
}point2D;
using namespace std;
#define maxNum 5
#define THREADS 32
__global__
void Testing(thrust::device_vector<point2D> *inputs) {
int idx = threadIdx.x + blockDim.x*blockIdx.x;
if (idx <inputs->size()) {
//static_cast<point2D>(inputs[idx]).x *=5; <<< no working
//static_cast<point2D>(inputs[idx]).y *=5; <<< no working
}
}
int main() {
srand((unsigned)time(NULL));
thrust::host_vector<point2D> alpha;
point2D inputs;
for (int i = 0; i < maxNum; i++) {
inputs.x = rand() % maxNum + 1;
inputs.y = rand() % maxNum + 1;
alpha.push_back(inputs);
}
for (int i = 0; i < alpha.size(); i++) {
printf("phase[%d] = xy(%d,%d)\n", i, alpha[i].x, alpha[i].y);
}
thrust::device_vector<point2D> beta = alpha;
dim3 threads(THREADS);
dim3 block(maxNum / threads.x);
Testing << < block, threads >> > (beta);
for(int i=0;i<beta.size();i++){
printf("phase[%d] = xy(%d,%d)\n",
i,
static_cast<point2D>(beta[i]).x,
static_cast<point2D>(beta[i]).y);
}
return 0;
}