Friday, February 6, 2015

How to configure and run cuda c with fortran in linux



Actually cuda can be programmed in Fortran and C . Cuda C is available in free whereas Cuda Fortran need to be paid for its use. Thus it is better to use cuda C.If our host code is
in C programming language then it is not a problem to use cuda C. However if we have a host code in fortran , then there are two option for us of using cuda fortran or cuda C. As cuda
C is freely available, we can use cuda C.

first we need to install nvida driver and cuda c toolkit . Installation of cuda c and nvidia driver is written in  earlier post here.

now the next step to implement cuda is first we need to call the C function from fortran using C wrapper as

1) create a  fortran file fortest.f95
PROGRAM fortest



! simple program which creates 2 vectors and adds them in a cuda function

IMPLICIT NONE

integer*4 :: i

integer*4, parameter :: N=8

real*4, Dimension(N) :: a, b

DO i=1,N

a(i)=i*1.0

b(i)=2.0

END DO

print *, 'a = ', (a(i), i=1,N)

CALL kernel_wrapper(a, b, N) // calling of C function from fortran

print *, 'a + 2 = ', (a(i), i=1,N)

END PROGRAM
2) create cuda file names as cudatest.cu



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cuda.h>
#include <cuda_runtime.h>
// simple kernel function that adds two vectors
__global__ void vect_add(float *a, float *b, int N)

{

int idx = threadIdx.x;
if (idx<N) a[idx] = a[idx] + b[idx];
}

// function called from main fortran program
extern "C" void kernel_wrapper_(float *a, float *b, int

*Np)
{

float *a_d, *b_d; // declare GPU vector copies

int blocks = 1; // uses 1 block of

int N = *Np; // N threads on GPU

// Allocate memory on GPU

cudaMalloc( (void **)&a_d, sizeof(float) * N );

cudaMalloc( (void **)&b_d, sizeof(float) * N );

// copy vectors from CPU to GPU

cudaMemcpy( a_d, a, sizeof(float) * N,

cudaMemcpyHostToDevice );



cudaMemcpy( b_d, b, sizeof(float) * N,

cudaMemcpyHostToDevice );

// call function on GPU

vect_add<<< blocks, N >>>( a_d, b_d, N);

// copy vectors back from GPU to CPU

cudaMemcpy( a, a_d, sizeof(float) * N,

cudaMemcpyDeviceToHost );

cudaMemcpy( b, b_d, sizeof(float) * N,

cudaMemcpyDeviceToHost );

// free GPU memory

cudaFree(a_d);

cudaFree(b_d);

return;

}





3)Compile Fortran file with gfortran complier
    $ gfortran –c fortest.f95

4)This create fortest.o file in current working directory

5)Compile CUDA file with nvcc complier
    $ nvcc –c cudatest.cu

6) This create cudatest.o file in current working directory

7)Link and compile these two object file

    $ gfortran –o <your executable file> fortest.o cudatest.o –L<your CUDA library path> -lcudart –lstdc++

          Example:

         final_file <my executable file> /usr/local/cuda-6.5/lib64 <my CUDA library path>

      $ gfortran –o final_file fortest.o cudatest.o –L /usr/local/cuda-
      6.5/lib64 -cudart –lstdc++

8)This will create executable file name as <your executable file>

    in current working directory

     Example:

     In my case final_file is created


9) Finally run this newly created executed file

./<your executable file>

  Example:

  $ ./final_file


OUTPUT


Source Code of Library Management Project in C

This is a simple library management project in C programming language. Here the file handlind is used to store the record of books as a database.

Library management project folder consists of
1) Library_Management.c
2) core_functions.h
3) general_function.h
4)variables.h

main function starts from source file Library_Management.c and the functions called in source file are defined in the general_function and core_function header files.



DOWNLOAD LIBRARY MANAGMENT IN C HERE

Thursday, February 5, 2015

How to install Nvidia driver and cuda in linux(centos 6.5)

If we are planning to install cuda in linux, The requirement are:
1)      Installation of nvidia driver
2)      Installation of cuda toolkit



Installation  of nvidia driver:
By default linux system doesnot have nvidia driver installed with it. In centos it has Nouveau driver installed. Thus first we need to install Nvidia driver. Installation of Nvidia driver requires some useful task.

The important thing we need to take care while installation of nvidia driver is that we need to have same version of kernel and kernel-devel of the particular linux installed.

First let’s check the kernel using command unname-r it gives the version of kenel
Second we need to check the kernel-devel version with command  (in centos) rpm –q  kernel-devel



If we have same kernel and kernel-devel we can directly proceed forward to install nvidia driver otherwise we need to update our kernel, kernel-devel and kernel-headers to the same version before we proceed to install nvidia driver.

Steps of updating to the requirements(same version)  in centos are
1)      yum groupinstall “Development Tools”
2)      yum install kenel-devel kernel-headers dkms
3)      yum install kernel
4)      mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak
5)      dracut -v /boot/initramfs-$(uname -r).img $(uname -r)
6)       reboot
then the new kernel will be seen during reboot , open the new kernel , now we need to blacklist nouveau in /etc/modprobe.d/blacklist.conf file. For this follow the steps
1)    Enter the command root mode
gedit  /etc/modeprobe.d/blacklist.conf and add the line blacklist nouveau in this file.
then download the nvidia driver , place it in appropriate place
enter init 3 in command, black screen appears , enter user name , password and log in as root user , move to the nvidia driver file , add permission to execute the file using
chmod  +x filename
and run the file as ./filename


this install the driver and finally enter init 5 to return to normal linux

finally now cuda file can be downloaded and installed using

./cuda_your_version.run

now test the cuda is installed or not by typing nvcc . If nvcc is recognized then cuda is installed successfully.