I am new to CUDA and trying to get a grasp for the basic so I apologize if something I ask or say sounds overly simple. I wrote some serial code in C for generating an array with random numbers and then finding the max within this array.
#include <stdio.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#define num 100000
int *arr,max = -1;
int getRand() {
double r1=rand()/(double)RAND_MAX; // Generates value between 0 & 1
return (r1 * num) + 1;
}
void generateRandom(int M) {
int i;
for(i=0;i<M;i++) {
arr[i] = getRand();
}
}
void getMax(int M) {
int i;
for(i=0;i<M;i++) {
if(arr[i] > max)
max = arr[i];
}
}
int main(int argc, char *argv[] ){
if (argc == 2) {
int M;
/* initialize random seed: */
srand (time(NULL));
M = atoi(argv[1]);
//int arr[M];
arr = (int*)calloc(M,sizeof(int));;
//printf("M = %d MAX = %d\n", M, RAND_MAX);
generateRandom(M);
getMax(M);
printf("Max value: %d",max);
}
else
printf("Invalid arguments.");
return 0;
}
I am now trying to convert this code into a simple CUDA program. I tried just making the generateRandom function run as a kernel but I am getting problems with the memory management.
#include <stdio.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <cuda.h>
#define num 100000
int *arr,max = -1;
int getRand() {
double r1=rand()/(double)RAND_MAX; // Generates value between 0 & 1
return (r1 * num) + 1;
}
void generateRandom(int M) {
int i;
for(i=0;i<M;i++) {
arr[i] = getRand();
}
}
__global__ void getMax(int M) {
int i;
for(i=0;i<M;i++) {
if(arr[i] > max)
max = arr[i];
}
}
int main(int argc, char *argv[] ){
if (argc == 2) {
int M;
/* initialize random seed: */
srand (time(NULL));
M = atoi(argv[1]);
//int arr[M];
arr = (int*)calloc(M,sizeof(int));
//printf("M = %d MAX = %d\n", M, RAND_MAX);
generateRandom(M);
getMax<<<1,1>>>(M);
printf("Max value: %d",max);
}
else
printf("Invalid arguments.");
return 0;
}
That code resulted in the following errors.
cudabasic.cu(23): warning: a host variable "arr" cannot be directly read in >a device function
cudabasic.cu(23): warning: a host variable "max" cannot be directly read in >a device function
cudabasic.cu(24): warning: a host variable "arr" cannot be directly read in >a device function
cudabasic.cu(24): warning: a host variable "max" cannot be directly written >in a device function
I googled the error and found out that the problem was that I was passing global variables to a kernel and thus the device wasnt able to read it. Following an online suggestion I tried to solve this by using pointers rather than passing actual variables but I am still getting errors.
#include <stdio.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <cuda.h>
#define num 100000
int *arr,max = -1;
int getRand() {
double r1=rand()/(double)RAND_MAX; // Generates value between 0 & 1
return (r1 * num) + 1;
}
void generateRandom(int M) {
int i;
for(i=0;i<M;i++) {
arr[i] = getRand();
}
}
__global__ void getMax(int M, int *dArr, int *dMax) {
int i = threadIdx.x;
int a = dArr[i];
for(i=0;i<M;i++) {
if(a > dMax)
dMax = a;
}
}
int main(int argc, char *argv[] ){
if (argc == 2) {
int M;
/* initialize random seed: */
srand (time(NULL));
M = atoi(argv[1]);
//int arr[M];
arr = (int*)calloc(M,sizeof(int));
devArr = (int*)cudaMalloc(M,sizeof(int));
//printf("M = %d MAX = %d\n", M, RAND_MAX);
generateRandom(M);
getMax<<<1,1>>>(M, arr, max);
printf("Max value: %d",max);
}
else
printf("Invalid arguments.");
return 0;
}
cudabasic.cu(24): error: operand types are incompatible ("int" and "int *")
cudabasic.cu(25): error: a value of type "int" cannot be assigned to an >entity of type "int *"
Can someone point me in the right direction of how to best go about doing this this?
I am new to CUDA and trying to get a grasp for the basic so I apologize if something I ask or say sounds overly simple.