TOTO HARYANTO

Sedikit goresan menebar manfaat …..

Generate id of thread on GPU

Posted by totoharyanto on February 16th, 2018

#include <stdio.h>
 #include <cuda.h>

#define LENGTH_OF_ARRAY 500
 #define THREADS 5
 #define BLOCKS 100
 // MAKE KERNEL FUNCTION

__global__ void print_real(int *a){
 int idx = blockIdx.x * blockDim.x + threadIdx.x;
 a[idx] = idx;
 }

__global__ void print_constant(int *a){
 int idx = blockIdx.x * blockDim.x + threadIdx.x;
 a[idx] = 7;
 }

__global__ void print_blockid(int *a){
 int idx = blockIdx.x * blockDim.x + threadIdx.x;
 a[idx] = blockIdx.x;
 }

__global__ void print_threadid(int *a){
 int idx = blockIdx.x * blockDim.x + threadIdx.x;
 a[idx] = threadIdx.x;
 }

int main()
 {
 int size = LENGTH_OF_ARRAY*sizeof(int); //DEFINE
 int i=0;
 // printf("Print constant\n");
 // printf("Print blockid\n");
 printf("Print threadid\n");
 int *a_h, *a_d;
 //memory allocation on host
 a_h = (int*)malloc(size);
 //memory allocation on device
 cudaMalloc((void**)&a_d, size);
 //call the kernel

// print_constant<<<BLOCKS,THREADS>>>(a_d);
 // print_blockid<<<BLOCKS,THREADS>>>(a_d);
 // print_threadid<<<BLOCKS,THREADS>>>(a_d);
 print_real<<<BLOCKS,THREADS>>>(a_d);

//copy from devie to host
 cudaMemcpy(a_h,a_d,size,cudaMemcpyDeviceToHost);

//print from host
 for(i=0;i<LENGTH_OF_ARRAY;i++)
 printf("%d ", a_h[i]);
 printf("\n\n");

cudaFree(a_d);

}

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>