title: How can I check for a malloc() failure within a CUDA kernel?
body: <p>This is a fairly self-explanatory question. Some background info is appended.</p>
  
  <p>How can I check for a <code>malloc()</code> failure within a CUDA kernel? I googled this and found nothing on what <code>malloc()</code> returns in a CUDA implementation.</p>
  
  <p>In addition, I have no idea how to signal back to the host that there was an error within a CUDA kernel. How can I do this?</p>
  
  <p>I thought one way would be to send an array of chars, one element for each kernel thread, and have the kernel place a <code>0x01</code> to signal an error and <code>0x00</code> for no error. Then the host could copy this memory back and check for any non zero bytes?</p>
  
  <p>But this seems like a waste of memory. Is there a better way? Something like cudaThrowError()? ... maybe? ...</p>
  
  <p>Appended:</p>
  
  <p>I am running into trouble with a cuda error: <code>GPUassert: the launch timed out and was terminated main.cu</code></p>
  
  <p>If you google this, you will find info for Linux users (who have hybrid graphics solutions) - the fix is sometimes to run with <code>optirun --no-xorg</code>.</p>
  
  <p>However in my case this isn't working.</p>
  
  <p>If I run my program for a small enough data set, I get no errors. For a large enough data set, but not too large, I have to prevent time out errors by passing the <code>--no-xorg</code> flag. For an <em>even larger dataset</em> I get timeout errors regardless of the <code>--no-xorg</code> flag.</p>
  
  <p>This hints to me that perhaps something else is going wrong?</p>
  
  <p>Perhaps a <code>malloc()</code> failure within my kernel if I run out of memory?</p>
  
  <p>I have checked my code and estimated memory usage - I don't <em>think</em> this is the problem, but I would like to check anyway.</p>