Nethaji
Nethaji

Reputation: 43

How to call “Cuda C” device routine from “Cuda Fortran Kernel/device routine”…?

I am trying to call a device routine in Cuda C from a Kernel in Cuda Fortran, but I am getting a linking error. Can someone please help me resolve the issue…?

File1:SortbyKey.f95

MODULE TEST
   USE CUDAFOR
   CONTAINS 
   ATTRIBUTES(GLOBAL) SUBROUTINE SORTING()
   IMPLICIT NONE

   INTERFACE
      ATTRIBUTES(DEVICE) SUBROUTINE SORTBYKEY(D,M) BIND(c, name='SORTBYKEY_')
         USE, INTRINSIC :: iso_c_binding
         INTEGER(KIND=4),VALUE::M
         REAL(KIND=8)::D(M)
      END SUBROUTINE SORTBYKEY
   END INTERFACE

   INTEGER(KIND=4),PARAMETER::N=10
   REAL(KIND=8)::B(N)

   B=(/100,98,65,50,54,43,40,30,20,10/)

   CALL SORTBYKEY(B,N)

   END SUBROUTINE SORTING

END MODULE TEST

PROGRAM MAIN
   USE TEST
   IMPLICIT NONE 
   INTEGER(KIND=4)::I

   CALL SORTING <<<1,1>>> ()
   I=CUDADEVICESYNCHRONIZE()
END PROGRAM MAIN

File2:SbyK.cu

#include <iostream>

#include <thrust/sort.h>
#include <thrust/execution_policy.h>

extern "C"
{
   __device__ void SORTBYKEY_(double *B, int N)
   {
      printf("\nB values are\n");
      for(int i=0;i<N;i++)
      {
         printf("%f\t",B[i]);
      }
     //thrust::sort(thrust::device, B, B + N);
   }
}

compiling:

nvfortran -Mcuda -c SortbyKey.f95
nvcc -c SbyK.cu

linking and error:

nvfortran -Mcuda -c++libs SbyK.o SortbyKey.o -o Sort
nvlink error   : Undefined reference to 'SORTBYKEY_' in 'SortbyKey.o'
pgacclnk: child process exit status 2: /opt/nvidia/hpc_sdk/Linux_x86_64/21.7/compilers/bin/tools/nvdd

My goal is to operate on the array B using the thrust library in the SORTBYKEY_ function. For example, if I launch 1000 threads, I want to sort array B in each thread using the thrust library.

Upvotes: 2

Views: 212

Answers (1)

Nethaji
Nethaji

Reputation: 43

By compiling with *cu file with -dc flag we can enable separation compilation and device linking.

compiling and linking

nvfortran -Mcuda -c SortbyKey.f95
nvcc -dc SbyK.cu
nvfortran -Mcuda -c++libs SbyK.o SortbyKey.o -o Sort

Upvotes: 1

Related Questions