Category: MyLab

Data Movement on GPU and Host

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

int main(void) {
float *a_h, *b_h; //host data
float *a_d, *b_d; // device data

int N=5, nBytes, i;

// declaration

nBytes = N*sizeof(float);
a_h = (float *)malloc(nBytes);
b_h = (float *)malloc(nBytes);
cudaMalloc((void **) &a_d, nBytes);
cudaMalloc((void **) &b_d, nBytes);


// set value of a_h

for (i=0;i<N;i++) a_h[i]=100.f * i;
// copy data from host to device
cudaMemcpy(a_d,a_h,nBytes,cudaMemcpyHostToDevice);
// copy data from device to device
cudaMemcpy(b_d,a_d,nBytes,cudaMemcpyDeviceToDevice);
// copy data from device to host
cudaMemcpy(b_h,b_d,nBytes,cudaMemcpyDeviceToHost);

for(i=0;i<N;i++) assert(a_h[i] == b_h[i]);
for (i=0;i<N;i++)
{
printf(" Isi dari variabel a_h adalah %f", a_h[i]);
printf("Isi dari variabel b_h adalah %f \n", b_h[i]);
}

free(a_h);free(b_h);cudaFree(a_d);cudaFree(b_d);

return 0;
}

Generate id of thread on GPU

Here the program to generate id of thread from GPU using CUDA architecture.

#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);

}

How to Install Medeley on LibreOffice

Mendeley is powerful document management software.  If you have project, paper or scientific writing you will cite and use the reference. Mendeley has ability to generate your citation easily.   Because I use LIbreOffice as Word Processing software,  here I will write the step by step  :

  1. Please download Mendeley on official website https://www.mendeley.com/
  2.  Install Mendeley Desktop and you have to registration with your email
  3. Run Mendeley on your computer/laptop .
  4.  Please choose Tools – Install LibreOffice Plugin

mendeley

5. Run  LibreOffice and choose Insert and  Insert citation. Choose citation style  (ex. IEEE

insert

(IEEE citation style or Amrican Standar)

style

 

6.  Wil display mendeley to select the paper will be cited

7.  Result on your Document

result

 

© 2019

Theme by Anders NorenUp ↑