Hi All,

Thanks in advance for looking in to my problem.

I am trying to run the sample program JCudaVectorAdd.java with JCudaVectorAddKernel.cu file. I am able to create the ptx file. But I am getting the following error during launch of kernel.

Exception in thread “main” jcuda.CudaException: CUDA_ERROR_LAUNCH_FAILED
at jcuda.driver.JCudaDriver.checkResult(JCudaDriver.java:282)
at jcuda.driver.JCudaDriver.cuCtxSynchronize(JCudaDriver.java:1794)
at JCudaVectorAdd.main(JCudaVectorAdd.java:97)

I am using JCuda-All-0.5.0-bin-windows-x86_64, cuda 5.0 for 64 bit and Visual Studo 2008 professional edition. I am able to run the sample cuda programs from the visual studio, which means that cuda is running fine. I am getting above problem with jcuda.


For Your Information I am adding the code i am running.

// JcudaVectorAdd.java

 * JCuda - Java bindings for NVIDIA CUDA driver and runtime API
 * http://www.jcuda.org
 * Copyright 2011 Marco Hutter - http://www.jcuda.org
import static jcuda.driver.JCudaDriver.*;

import java.io.*;

import jcuda.*;
import jcuda.driver.*;

 * This is a sample class demonstrating how to use the JCuda driver
 * bindings to load and execute a CUDA vector addition kernel.
 * The sample reads a CUDA file, compiles it to a PTX file
 * using NVCC, loads the PTX file as a module and executes
 * the kernel function. <br />
public class JCudaVectorAdd
     * Entry point of this sample
     * @param args Not used
     * @throws IOException If an IO error occurs
    public static void main(String args[]) throws IOException
        // Enable exceptions and omit all subsequent error checks

        // Create the PTX file by calling the NVCC
        String ptxFileName = preparePtxFile("K:\\JCuda_Samples\\JCUDAVectorAdd\\JCudaVectorAddKernel.cu");

        // Initialize the driver and create a context for the first device.
        CUdevice device = new CUdevice();
        cuDeviceGet(device, 0);
        CUcontext context = new CUcontext();
        cuCtxCreate(context, 0, device);
        // Load the ptx file.
        CUmodule module = new CUmodule();
        cuModuleLoad(module, ptxFileName);

        // Obtain a function pointer to the "add" function.
        CUfunction function = new CUfunction();
        cuModuleGetFunction(function, module, "add");

        int numElements = 100000;

        // Allocate and fill the host input data
        float hostInputA[] = new float[numElements];
        float hostInputB[] = new float[numElements];
        for(int i = 0; i < numElements; i++)
            hostInputA** = (float)i;
            hostInputB** = (float)i;

        // Allocate the device input data, and copy the
        // host input data to the device
        CUdeviceptr deviceInputA = new CUdeviceptr();
        cuMemAlloc(deviceInputA, numElements * Sizeof.FLOAT);
        cuMemcpyHtoD(deviceInputA, Pointer.to(hostInputA),
            numElements * Sizeof.FLOAT);
        CUdeviceptr deviceInputB = new CUdeviceptr();
        cuMemAlloc(deviceInputB, numElements * Sizeof.FLOAT);
        cuMemcpyHtoD(deviceInputB, Pointer.to(hostInputB),
            numElements * Sizeof.FLOAT);

        // Allocate device output memory
        CUdeviceptr deviceOutput = new CUdeviceptr();
        cuMemAlloc(deviceOutput, numElements * Sizeof.FLOAT);

        // Set up the kernel parameters: A pointer to an array
        // of pointers which point to the actual values.
        Pointer kernelParameters = Pointer.to(
            Pointer.to(new int[]{numElements}),

        // Call the kernel function.
        int blockSizeX = 256;
        int gridSizeX = (int)Math.ceil((double)numElements / blockSizeX);
            gridSizeX,  1, 1,      // Grid dimension
            blockSizeX, 1, 1,      // Block dimension
            0, null,               // Shared memory size and stream
            kernelParameters, null // Kernel- and extra parameters

        // Allocate host output memory and copy the device output
        // to the host.
        float hostOutput[] = new float[numElements];
        cuMemcpyDtoH(Pointer.to(hostOutput), deviceOutput,
            numElements * Sizeof.FLOAT);

        // Verify the result
        boolean passed = true;
        for(int i = 0; i < numElements; i++)
            float expected = i+i;
            if (Math.abs(hostOutput** - expected) > 1e-5)
                    "At index "+i+ " found "+hostOutput**+
                    " but expected "+expected);
                passed = false;
        System.out.println("Test "+(passed?"PASSED":"FAILED"));

        // Clean up.

     * The extension of the given file name is replaced with "ptx".
     * If the file with the resulting name does not exist, it is
     * compiled from the given file using NVCC. The name of the
     * PTX file is returned.
     * @param cuFileName The name of the .CU file
     * @return The name of the PTX file
     * @throws IOException If an I/O error occurs
    private static String preparePtxFile(String cuFileName) throws IOException
        int endIndex = cuFileName.lastIndexOf('.');
        if (endIndex == -1)
            endIndex = cuFileName.length()-1;
        String ptxFileName = cuFileName.substring(0, endIndex+1)+"ptx";
        File ptxFile = new File(ptxFileName);
        if (ptxFile.exists())
            return ptxFileName;

        File cuFile = new File(cuFileName);
        if (!cuFile.exists())
            throw new IOException("Input file not found: "+cuFileName);
        String modelString = "-m"+System.getProperty("sun.arch.data.model");
        String command =
            "nvcc " + modelString + " -ptx "+
            cuFile.getPath()+" -o "+ptxFileName;

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

        String errorMessage =
            new String(toByteArray(process.getErrorStream()));
        String outputMessage =
            new String(toByteArray(process.getInputStream()));
        int exitValue = 0;
            exitValue = process.waitFor();
        catch (InterruptedException e)
            throw new IOException(
                "Interrupted while waiting for nvcc output", e);

        if (exitValue != 0)
            System.out.println("nvcc process exitValue "+exitValue);
            throw new IOException(
                "Could not create .ptx file: "+errorMessage);

        System.out.println("Finished creating PTX file");
        return ptxFileName;

     * Fully reads the given InputStream and returns it as a byte array
     * @param inputStream The input stream to read
     * @return The byte array containing the data from the input stream
     * @throws IOException If an I/O error occurs
    private static byte[] toByteArray(InputStream inputStream)
        throws IOException
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte buffer[] = new byte[8192];
        while (true)
            int read = inputStream.read(buffer);
            if (read == -1)
            baos.write(buffer, 0, read);
        return baos.toByteArray();


// JcudaVectorAddKernel.cu

extern "C"
__global__ void add(int n, float *a, float *b, float *sum)
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i<n)
        sum** = a** + b**;



I just had a short look at the code… is it right that there is no significant difference between the code that you posted and the sample from the website?

Unfortunately, “CUDA_ERROR_LAUNCH_FAILED” is rather unspecific. The documentation only says

An exception occurred on the device while executing a kernel. Common causes include dereferencing an invalid device pointer and accessing out of bounds shared memory. The context cannot be used, so it must be destroyed (and a new one should be created). All existing device memory allocations from this context are invalid and must be reconstructed if the program is to continue using CUDA.

which does not apply to to original sample.

In doubt, I’ll have to take a closer look at this maybe beginning of next week.