HPMC User Guide v 1.00
© 2026 Bassem W. Jamaleddine
4-4
FORTRAN programs can link to external CUDA functions. In this chapter we shows two FORTRAN programs: the first VECADDfort.f95 that links to CUDA external function to add two vectors, and the other AxBfort.f95 that links to CUDA external function to multiply two matrices.
■ FORTRAN Program Calling CUDA to Add Two Vectors
FORTRAN program can call CUDA routines by linking to CUDA object files. The following shows an example on how to compile a FORTRAN program that calls an external CUDA function through the cuda_kernel_wrapper method.
1. PROGRAM VECADDfort 2. 3. ! simple program which creates 2 vectors and adds them in an 4. ! external cuda function 5. 6. IMPLICIT NONE 7. 8. integer*4 :: I 9. integer*4, parameter :: N=8 10. real*4, Dimension(N) :: A, B 11. 12. DO I = 1, N 13. A(I) = I * 1.0 14. B(I) = 1.5 15. END DO 16. 17. print *, 'A = ', (A(I), I=1,N) 18. 19. CALL cuda_kernel_wrapper(A, B, N) 20. 21. print *, 'A + 1.5 = ', (A(I), I=1,N) 22. 23. END PROGRAM
CUDA program with an external function callable from FORTRAN programs.
1. #include <stdio.h> 2. #include <stdlib.h> 3. #include <string.h> 4. #include <cuda.h> 5. #include <cuda_runtime.h> 6. 7. // kernel function to add two vectors 8. __global__ void vect_add(float *a, float *b, int N) { 9. int idx = threadIdx.x; 10. if (idx<N) a[idx] = a[idx] + b[idx]; 11. } 12. 13. // function called from main's fortran program 14. extern "C" void cuda_kernel_wrapper_(float *a, float *b, int *Np) { 15. float *a_d, *b_d; // device GPU vectors 16. 17. int blocks = 1; // uses 1 block of 18. int N = *Np; // N threads on GPU 19. 20. // allocate memory on GPU 21. cudaMalloc( (void **)&a_d, sizeof(float) * N ); 22. cudaMalloc( (void **)&b_d, sizeof(float) * N ); 23. 24. // copy vectors from CPU to GPU 25. cudaMemcpy( a_d, a, sizeof(float) * N, cudaMemcpyHostToDevice ); 26. cudaMemcpy( b_d, b, sizeof(float) * N, cudaMemcpyHostToDevice ); 27. 28. // call function on GPU 29. vect_add<<< blocks, N >>>( a_d, b_d, N); 30. 31. // copy vectors back from GPU to CPU 32. cudaMemcpy( a, a_d, sizeof(float) * N, cudaMemcpyDeviceToHost ); 33. cudaMemcpy( b, b_d, sizeof(float) * N, cudaMemcpyDeviceToHost ); 34. 35. // free GPU memory 36. cudaFree(a_d); 37. cudaFree(b_d); 38. }
Makefile to compile the program VECADDfort.f95
1. Test: VECADDfort.f95 extVecAdd.o 2. gfortran -L /usr/local/cuda/lib -I /usr/local/cuda/include -lcudart -lcuda VECADDfort.f95 extVecAdd.o 3. extVecAdd.o: extVecAdd.cu 4. nvcc -c -O3 extVecAdd.cu 5. clean: 6. rm a.out extVecAdd.o 7.
Compiling and running the program VECADDfort.
06:45 root@HPMC: /tmp/cudafort # make nvcc -c -O3 extVecAdd.cu gfortran -L /usr/local/cuda/lib -I /usr/local/cuda/include -lcudart -lcuda VECADDfort.f95 extVecAdd.o 06:46 root@HPMC: /tmp/cudafort # ./a.out A = 1.00000000 2.00000000 3.00000000 4.00000000 5.00000000 6.00000000 7.00000000 8.00000000 A + 1.5 = 2.50000000 3.50000000 4.50000000 5.50000000 6.50000000 7.50000000 8.50000000 9.50000000
■ FORTRAN Program Calling CUDA to Multiply Matrices
FORTRAN program can call CUDA routines by linking to CUDA object files. The following shows an example on how to compile a FORTRAN program that calls an external CUDA function to multiply two matrices.
1. PROGRAM AxBfort 2. 3. ! simple FORTRAN program to multiply two matrices calling 4. ! external cuda function 5. 6. IMPLICIT NONE 7. 8. integer*4 :: I 9. integer*4, parameter :: N=3 10. real*4, Dimension(N*N) :: A, B, C 11. 12. DO I=1,N*N 13. A(I) = 1.0 14. B(I) = I*1.0 15. C(I) = 0.0 16. END DO 17. 18. CALL matrix_multiplication(A, B, C, N); 19. 20. print *, 'C = ', (C(I), I=1,N*N) 21. 22. END PROGRAM
CUDA program with an external function callable from FORTRAN programs.
#include <stdio.h> #include <stdlib.h> #include <cuda.h> #include <cuda_runtime.h> // matrix multiplication kernel - thread specification __global__ void MatrixMulKernel(float *Ad, float *Bd, float *Cd, int width) { // 2D Thread ID int tx = threadIdx.x; int ty = threadIdx.y; // Cvalue stores the Cd element that is computed by the thread float Cvalue = 0; for(int k = 0; k < width ; ++k) { float Adelement = Ad[ty*width + k]; float Bdelement = Bd[k*width + tx]; Cvalue += (Adelement*Bdelement); } Cd[ty*width + tx] = Cvalue; } // function called from main's fortran program extern "C" void matrix_multiplication_(float *A, float *B, float *C, int *Np) { int width = *Np; int size = width*width*sizeof(float); float *Ad, *Bd, *Cd; // transfer A and B to device memory cudaMalloc((void**)&Ad, size); cudaMemcpy(Ad,A,size,cudaMemcpyHostToDevice); cudaMalloc((void**)&Bd, size); cudaMemcpy(Bd,B,size,cudaMemcpyHostToDevice); // allocate P on the device cudaMalloc((void**)&Cd,size); // setup the execution configuration dim3 dimBlock(width,width); dim3 dimGrid(1,1); // launch the device computation threads MatrixMulKernel<<<dimGrid,dimBlock>>>(Ad,Bd,Cd,width); // transfer P from device to host cudaMemcpy(C,Cd,size,cudaMemcpyDeviceToHost); // free device matrices cudaFree(Ad); cudaFree(Bd); cudaFree(Cd); }
Makefile to compile the program VECADDfort.f95
1. Test: AxBfort.f95 extAxBf.o 2. gfortran -L /usr/local/cuda/lib -I /usr/local/cuda/include -lcudart -lcuda AxBfort.f95 extAxBf.o 3. extAxBf.o: extAxBf.cu 4. nvcc -c -O3 extAxBf.cu 5. clean: 6. rm a.out extAxBf.o 7.
Compiling and running the program AxBfort.
07:31 root@HPMC: /tmp/cudafort # make nvcc -c -O3 extAxBf.cu gfortran -L /usr/local/cuda/lib -I /usr/local/cuda/include -lcudart -lcuda AxBfort.f95 extAxBf.o 07:31 root@HPMC: /tmp/cudafort # ./a.out 0 12.000000 1 15.000000 2 18.000000 3 12.000000 4 15.000000 5 18.000000 6 12.000000 7 15.000000 8 18.000000 C = 12.0000000 15.0000000 18.0000000 12.0000000 15.0000000 18.0000000 12.0000000 15.0000000 18.0000000