Why does my CUDA kernel crash (unspecified launch failure) with a different dataset size?
<p>I have a kernel to calculate different elements of a matrix, based on their position (diagonal or off-diagonal). The kernel works as expected when calculating matrices of sizes: </p>

<ul>
<li>14 x 14 (I understand this is small and does not make proper use of the GPU resources but this was purely for testing purposes to ensure results were correct)</li>
<li>118 x 118, and</li>
<li>300 x 300</li>
</ul>

<p>However, when I am trying to calculate a matrix of size 2383 x 2383, the kernel crashes. Specifically, the error "Unspecified launch failure" is thrown on the cudaMemcpy() line to return results from device to host. From research, I understand that this error usually arises in the case of an out of bounds memory access (e.g. in an array), however, what I don't get is that it works for the three previous cases but not for the 2383 x 2383 case. The kernel code is shown below:</p>

<pre><code>__global__ void createYBus(float *R, float *X, float *B, int numberOfBuses, int numberOfBranches, int *fromBus, int *toBus, cuComplex *y)
{
    int rowIdx = blockIdx.y*blockDim.y + threadIdx.y;
    int colIdx = blockIdx.x*blockDim.x + threadIdx.x;
    int index = rowIdx*numberOfBuses + colIdx;
    if (rowIdx&lt;numberOfBuses &amp;&amp; colIdx&lt;numberOfBuses)
    {
        for (int i=0; i&lt;numberOfBranches; ++i)
        {
            if (rowIdx==fromBus[i] &amp;&amp; colIdx==fromBus[i]) { //diagonal element
                y[index] = cuCaddf(y[index], make_cuComplex((R[i]/((R[i]*R[i])+(X[i]*X[i]))), (-(X[i]/((R[i]*R[i])+(X[i]*X[i])))+ (B[i]/2))));
            }
            if (rowIdx==toBus[i] &amp;&amp; colIdx==toBus[i]) { //diagonal element
                y[index] = cuCaddf(y[index], make_cuComplex((R[i]/((R[i]*R[i])+(X[i]*X[i]))), (-(X[i]/((R[i]*R[i])+(X[i]*X[i])))+ (B[i]/2))));
            }
            if (rowIdx==fromBus[i] &amp;&amp; colIdx==toBus[i]) { //off-diagonal element
                y[index] = make_cuComplex(-(R[i]/((R[i]*R[i])+(X[i]*X[i]))), X[i]/((R[i]*R[i])+(X[i]*X[i])));
            }
            if (rowIdx==toBus[i] &amp;&amp; colIdx==fromBus[i]) { //off-diagonal element
                y[index] = make_cuComplex(-(R[i]/((R[i]*R[i])+(X[i]*X[i]))), X[i]/((R[i]*R[i])+(X[i]*X[i])));
            }
        }
    }
}
</code></pre>

<p>Global memory allocations are done via calls to cudaMalloc(). The allocations made in the code are as follows:</p>

<pre><code>cudaStat1 = cudaMalloc((void**)&amp;dev_fromBus, numLines*sizeof(int));
cudaStat2 = cudaMalloc((void**)&amp;dev_toBus, numLines*sizeof(int));
cudaStat3 = cudaMalloc((void**)&amp;dev_R, numLines*sizeof(float));
cudaStat4 = cudaMalloc((void**)&amp;dev_X, numLines*sizeof(float));
cudaStat5 = cudaMalloc((void**)&amp;dev_B, numLines*sizeof(float));
cudaStat6 = cudaMalloc((void**)&amp;dev_y, numberOfBuses*numberOfBuses*sizeof(cuComplex));
cudaStat7 = cudaMalloc((void**)&amp;dev_Pd, numberOfBuses*sizeof(float));
cudaStat8 = cudaMalloc((void**)&amp;dev_Qd, numberOfBuses*sizeof(float));
cudaStat9 = cudaMalloc((void**)&amp;dev_Vmag, numberOfBuses*sizeof(float));
cudaStat10 = cudaMalloc((void**)&amp;dev_theta, numberOfBuses*sizeof(float));
cudaStat11 = cudaMalloc((void**)&amp;dev_Peq, numberOfBuses*sizeof(float));
cudaStat12 = cudaMalloc((void**)&amp;dev_Qeq, numberOfBuses*sizeof(float));
cudaStat13 = cudaMalloc((void**)&amp;dev_Peq1, numberOfBuses*sizeof(float));
cudaStat14 = cudaMalloc((void**)&amp;dev_Qeq1, numberOfBuses*sizeof(float));
...
...
cudaStat15 = cudaMalloc((void**)&amp;dev_powerMismatch, jacSize*sizeof(float));
cudaStat16 = cudaMalloc((void**)&amp;dev_jacobian, jacSize*jacSize*sizeof(float));
cudaStat17 = cudaMalloc((void**)&amp;dev_stateVector, jacSize*sizeof(float));
cudaStat18 = cudaMalloc((void**)&amp;dev_PQindex, jacSize*sizeof(int));
</code></pre>

<p>where cudaStatN are of type cudaError_t to catch errors. The last four allocations were done later on in the code and are for another kernel. However these allocations were done before the kernel in question was called.</p>

<p>The launch parameters are as follows:</p>

<pre><code>dim3 dimBlock(16, 16); //number of threads 
dim3 dimGrid((numberOfBuses+15)/16, (numberOfBuses+15)/16);  //number of blocks

//launch kernel once data has been copied to GPU
createYBus&lt;&lt;&lt;dimGrid, dimBlock&gt;&gt;&gt;(dev_R, dev_X, dev_B, numberOfBuses, numLines, dev_fromBus, dev_toBus, dev_y);

//copy results back to CPU
cudaStat6 = cudaMemcpy(y_bus, dev_y, numberOfBuses*numberOfBuses*sizeof(cuComplex), cudaMemcpyDeviceToHost);
if (cudaStat6 != cudaSuccess) {
    cout&lt;&lt;"Device memcpy failed"&lt;&lt;endl;
    cout&lt;&lt;cudaGetErrorString(cudaStat6)&lt;&lt;endl;
    return 1;
}
</code></pre>

<p>I removed the timing code just to show the block and grid dimensions and error checking technique used.</p>

<p>I also have a host (C++ code) version of this function and I'm passing the data to both functions and then comparing results, firstly, to ensure the kernel produces correct results, and secondly in terms of execution time to compare performance. I have double checked the data for the 2383 x 2383 case (it's being read in from a text file and copied to global memory) and I'm not finding any anomalies in array accesses/indexing. </p>

<p>I'm using Visual Studio 2010, so I tried using Nsight to find the error (I'm not too well-versed with Nsight). The summary report overview states: "There was 1 runtime API call error reported. (Please see the CUDA Runtime API Calls report for further information). In the list of runtime API calls, cudaMemcpy returns error 4 - not sure if the Thread ID (5012) is of any significance in the table - this number varies with every run. CUDA memcheck tool (in the command line) returns the following:</p>

<pre><code>Thank you for using this program
========= Program hit cudaErrorLaunchFailure (error 4) due to "unspecified launch failure" on CUDA API call to cudaMemcpy.
=========     Saved host backtrace up to driver entry point at error
=========
========= ERROR SUMMARY: 1 error
</code></pre>

<p>I know my kernel isn't the most efficient as there are many global memory accesses. Why is the kernel crashing for this larger matrix? Is there an out of bounds array access that I'm missing? Any assistance would be greatly appreciated.</p>