Problem with compiling Cubin file in windows !

Hello marco

I am compiling .cubin file in windows. But as you know nvcc does not run in windows command line. So, I am bit confused that how to do this. Actualy what I want to do is :

-I have a Java code and I have to run it from commnad line (not in Net Beans or any other java IDE).
-Then this code contains JCuda part in which it will generate a .cubin file by calling nvcc to generate it.
-Now, problem lies here that what to do now? because windows command line does not recognize nvcc because as far as I know unlike linux windows does not have any builtin c compiler like gcc/g++

Any solution please?

Thanks in advance.

In general, the NVCC can not compile anything “standalone”. There always has to be one of the supported C compilers in the background. When having installed, for example, MS Visual Studio, the required environment variables should already have been set automatically. Thus, when MS Visual Studio is installed, it should in general be possible to execute the NVCC directly from the command line.

I once started creating a set of classes which should provide an object-oriented abstraction layer for CUDA (delegating the calls directly to JCuda, of course). Although this project lies dormant at the moment, here is a short snippet showing how I compiled CUBIN files by calling NVCC from Java during runtime:

Process process = null;
try
{
    String command = "nvcc -cubin "+cuFile.getPath()+" -o "+cubinFileName;
    System.out.println("Executing
"+command);
    process = Runtime.getRuntime().exec(command);
}
catch (IOException e)
{
    throw new RuntimeException("Could not execute nvcc compiler", e);
}

String errorMessage = new String(toByteArray(process.getErrorStream()));
String outputMessage = new String(toByteArray(process.getInputStream()));
int exitValue = 0;
try
{
    exitValue = process.waitFor();
}
catch (InterruptedException e)
{
    Thread.currentThread().interrupt();
    throw new RuntimeException("Interrupted while waiting for nvcc output", e);
}

System.out.println("nvcc process exitValue "+exitValue);

EDIT >> The ‘toByteArray’ method just reads an inputStream until its end, and returns the contents as a byte array <<

The command line should actually be something like


nvcc -cubin theKernel.cu -o theKernel.cubin

This basically worked, when MSVS is installed. I assume that you’re using something similar in your case.

If no supported C compiler is installed (or it cannot be found, because the path to its executable is not located in the PATH environment variable), calling the NVCC in the way described above will most likely return an ‘exitValue’ which is !=0. In this case, I could not think of any way to create a CUBIN file at runtime…

Maybe someone has a good idea what to do in this case, except for printing a helpful error message and urging the user to install an appropriate compiler…

Hello Marco

I have MS VS2005 installed I copied this code and gave name of my .cu and .cubin file and ran the code.
MS VS2005 path is already in system PATH. But when I ran this code returned exit value -1 (means error).
Please guide me if I am wrong.

Thanks in advance

(here is the code…)

import java.net.;
import java.io.
;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class testCubin {

public static void main (String []args) {
	
	

  Process process = null;

  try

  {

      String command = "nvcc -cubin "+"JCudaCubinSample_kernel.cu -o "+"createdCubin.cubin";

      System.out.println("Executing

"+command);

      process = Runtime.getRuntime().exec(command);

  }

  catch (Exception e)

  {

      throw new RuntimeException("Could not execute nvcc compiler", e);

 }

  

  String errorMessage = new String(process.getErrorStream().toString());

  String outputMessage = new String(process.getInputStream().toString());

  int exitValue = 0;

  try

  {

      exitValue = process.waitFor();

  }

 catch (InterruptedException e)

  {

      Thread.currentThread().interrupt();

      throw new RuntimeException("Interrupted while waiting for nvcc output", e);

  }

   

  System.out.println("nvcc process exitValue "+exitValue);
  
  
  
}

}

The line


String errorMessage = new String(process.getErrorStream().toString());

will not work as expected: The process.getErrorStream/getInputStream methods have some constraints, especially that their contents has to be read completely until the process can finish. (Simply call toString will return a String representation of the InputStream Object, but will not read the contents of the stream…)

The “toByteArray” Method I used in the above sample might roughly look like this

private static byte[] toByteArray(InputStream inputStream)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte buffer[] = new byte[1024];
    while (true)
    {
        int read = inputStream.read(buffer);
        if (read < 0) break;
        baos.write(buffer,0,read);
    }
    return baos.toByteArray();
}

(just written quickly and untested, but should do…)

If you are using this, you can also print the output that NVCC produces by printing the Strings that are created with

String errorMessage = new String(toByteArray(process.getErrorStream()));
String outputMessage = new String(toByteArray(process.getInputStream()));

Especially, the error message might be relevant.

Concerning your example, I currently cannot test it, but if obtaining and printing the error- and output messages as described above does not help, I can provide an example in a few days.

Hello Marco,

I got my problem solved. I was successfully able to generate .cubin file by calling nvcc from windows command line. What I did…

-The script named „vsvars32.bat“ located in the „C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools“
(I am using default directory) must be running before calling nvcc.

  • After running this script, I explicitly tell the nvcc where the MSVC compiler „cl.exe“ is located by using a
    switch „-ccbin“.
    -Finally I ran my .cu file and it worked !!! :slight_smile:

C:\Users\Jahanzeb\Desktop>nvcc -ccbin „C:\Program Files\Microsoft Visual Studio
9.0\VC\bin“ -cubin JCudaCubinSample_kernel.cu -o JCudaCubinSample_kernel.cubin

  • Thank God I got rid of the error „cl version not supported…etc“…

Thank you marco for your support.

Great. This might be helpful for others. I think the intention to compile CUBIN files at runtime is quite common (You could think about writing a „CUDA Code Generator“ in Java :smiley: )

I also remember some issue with the „vsvars32.bat“ - bit as far as I remember, it only has to be executed once and it permanently sets some required environment variables, doesn’t it? :confused:

right.

[QUOTE=Jahanzeb]Hello Marco,

I got my problem solved. I was successfully able to generate .cubin file by calling nvcc from windows command line. What I did…

-The script named „vsvars32.bat“ located in the „C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools“
(I am using default directory) must be running before calling nvcc.

  • After running this script, I explicitly tell the nvcc where the MSVC compiler „cl.exe“ is located by using a
    switch „-ccbin“.
    -Finally I ran my .cu file and it worked !!! :slight_smile:

C:\Users\Jahanzeb\Desktop>nvcc -ccbin „C:\Program Files\Microsoft Visual Studio
9.0\VC\bin“ -cubin JCudaCubinSample_kernel.cu -o JCudaCubinSample_kernel.cubin

  • Thank God I got rid of the error „cl version not supported…etc“…

Thank you marco for your support.[/QUOTE]

Thank you very much. I can successfully follow your way to create the cubin file.:smiley: