A question on the use of cuda c from cuda fortran

Hi all,

I was writing some code with cuda fortran, and wanted to use the thrust/sort funcftion.
A simple cuda c code is used as (according to the FORTRAN CUDA LIBRARY INTERFACES, section 1.7):

exts.cu
#include <cuda_runtime_api.h>
#include <thrust/sort.h>

extern “C”
{
void thr_SbyK(keys, N, values)
{
thrust::sort_by_key(keys, keys + N, values);
}
}

when compiling it with pgice using nvcc -c -cuda exts.cu

it shows an error
D:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/…/…/…/VC/INCLUDE\crtdefs.h(10): fatal error C1083: Cannot open include file: “corecrt.hâ€�: No such file or directory

to solve this, I right click the pgi.bat and changed the path ‘…Microsoft Visual Studio 14.0\VC.…’ to ‘…Microsoft Visual Studio 10.0\VC.…’ and it seems work.

Then another error shows:
exts.cu(2) : fatal error C1083: Cannot open include file:“thrust/sort.h�: No such file or directory
To solve this I right click the pgi.bat and added the path:
set PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include;%PATH%
set PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\thrust;%PATH%

It doesn’t work. I also tryed to use include in the makefile, and it also doesn’t work. Actually I’m not quite clear how to use include in the makefile and what is the difference between using path and using include.

Could someone help me? Thanks a lot!

Yi

Hi Yi,

PGI only ships the portions of CUDA which we need to support CUDA Fortran. This does not include the Thrust libraries. For Thrust, you’ll need to install the CUDA SDK and then either use the DOS command shell to compile your CUDA routine, or modify the pgi.bat file to include PATHs to you’re CUDA SDK installation. Information on installing CUDA on Windows can be found here:

Note that PVF is a Fortran plugin for VS. The error you’re getting with VS looks to me to be a configuration issue which most likely will get resolved after installing the CUDA SDK. The CUDA installation guide discusses VS configuration.

-Mat

Hi Mat,

Thank you very much for reply.
I have installed the cuda 8.0 on my computer and it works well with the cuda c++ in vs 2010.
However I used pgice so when using cuda fortran I have to use the command window.
I did add the path of the install folder as:
set PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include;%PATH%
set PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include\thrust;%PATH%
but it still doesn’t work.

Did I add it in a wrong way… ?

The PATH environment is used by the shell to find executables not include paths.

Try using the “-I” flag (or multiple -I if there are multiple include directories) on your compilation to indicate to the nvcc compiler where the include files are located. For example something like:

nvcc -c -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include" -cuda exts.cu

Cuda C programs are only compiled and linked successfully by the
Nvidia components (nvcc). PGI does not provide an environment for
CUDA C programs to be compiled. It provides a CUDA Fortran
compiler that converts Fortran to CUDA code, and then runs a
fairly simple version of nvcc to compile and link the CUDA Code.

We also convert OpenACC C++ and Fortran programs to
CUDA programs, and use th esame simple version of nvcc.

But there is no attempt made to compile cuda C programs with
anything other than the Nvidia programming tools.



dave

Hi guys,

Thank you very much for your replies.
I’ve got more understandings on the path and include. Path is for the exes and include is for the code or pre-compiled objs, right? then what about lib? is it for dll files?
I’ll try again when I get back to my office tomorrow and let you know if it successes.

-L\path\to\library\lib sets the library path(s) to look at before
the ones the drivers (pgcc pgfortran) add.


You can add
-v
to see the details of what the drivers do when building code.

Just to see what they do, without doing anything, use

-dryrun

-lxxx
means to look for
libxxx.lib
or
libxxx.dll

in one of the directories searched.

dave

Hi guys,

I finaly get the code compiled successfully.

However it seems the program just get into the cuda c function and return to system.

Could you have a look at the code:

  1. This is the cuda c file
    #include <thrust/device_vector.h>
    #include <thrust/copy.h>
    #include <thrust/sort.h>
    extern “C”
    {
    void thr_SbyK(int *KEY, int N, int *ARRAY)
    {
    thrust::device_ptr dev_ptr1(KEY);
    thrust::device_ptr dev_ptr2(ARRAY);
    thrust::sort_by_key(dev_ptr1, dev_ptr1+N, dev_ptr2);
    }
    }

  2. this is the interface
    INTERFACE SORTBYGN
    SUBROUTINE SORTBYGN_thr(KEY,N,ARRAY)
    & bind(C,name=‘thr_SbyK’)
    INTEGER,DEVICE,DIMENSION()::KEY
    INTEGER,DEVICE::N
    INTEGER,DEVICE,DIMENSION(
    )::ARRAY
    END SUBROUTINE
    END INTERFACE

  3. this is the calling
    write(,) ‘p3’
    D_PID = D_PID0
    CALL SORTBYGN(D_GNC,D_NP,D_PID)
    write(,) ‘p4’
    ! D_GNC and D_PID are device arraies and D_NP is a device integer

After I run the program, it just shows p3 and stop running.

The devil is in the details, and there is much lost when you send
code fragments and expect us to reconstruct your program and what went wrong.

Please either post complete examples in the User Forum, or send
them to trs@pgroup.com for privacy.

thanks,
dave