Hello everyone
I am trying to call CUDA from FORTRAN. I have tried to call CUDA the same way as we call CUDA from “C”, but it did not work out. I have read a number of articles online which said we can call C from FORTRAN and make C call CUDA. Could anyone help me. I have got my programs written but it does not work, it gives me errors when I try to make the .exe file. Here are the simple programs I am trying to generate the .exe for
The Header file b.h
#ifndef _B_H
#define _B_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<cuda.h>
#include<cuda_runtime.h>
#endif
The FORTRAN fcode.F
program fcode
integer a1(4,4)
integer a2(4,4)
integer r(4,4)
integer i,j
call ccode()
do i=1,4
do j=1,4
a1(i,j)=i
a2(i,j)=i
r(i,j)=0
end do
end do
write(UNIT=*,FMT=*) 'This is the Fortarn Code'
do i=1,4
do j=1,4
r(i,j) = a1(i,j) + a2(i,j)
end do
end do
end
The C code ccode.c
#include “b.h”
extern void kernel_wrapper();
ccode_()
{
printf(“This is the C code”);
kernel_wrapper();
}
The CUDA code cucode.cu
#include “b.h”
extern “C” void kernel_wrapper();
global void kernel()
{
}
void kernel_wrapper()
{
dim3 dimBlock(4,4);
dim3 dimGrid(1,1);
kernel <<<dimGrid, dimBlock >>> ();
printf(“This is the CUDA code”);
}
The make file Makefile
run: fcode.o ccode.o cucode.o
gcc -L/usr/local/cuda/lib -lcudart -o run fcode.o ccode.o cucode.o
fcode.o: fcode.o
gfortran -c -o fcode.o
ccode.o: ccode.o
gcc -I/usr/local/cuda/lib -c -o ccode.o ccode.c
cucode.o: cucode.cu
nvcc -c -o cucode.o cucode.cu