Are objects destroyed at the end of a kernel?

Hi³ everybody :wave: ,

I have a new question due to an example that disturbs me:

class titi

{

  int i;

  public:

	__device__

	titi(int j):i(0)

	{

	  i=j;

	}

	__device__

	int _i()

	{

	  return i;

	}

};

__global__ void mykernel(float * A1, float * R)

{

titi * too;	

	if(A1[0])

	{

	  titi ti(5);

	  too=&ti;

	}

	R[0]=too->_i();

}

int main()

{

  int h;

		float A1[]={1,2,3,4,5,6,7,8,9};

		float R[9];

		int taille_mem=sizeof(float) * 9;

		

		float * a1_device;

		float * r_device;

		cudaMalloc ( (void**) &a1_device, taille_mem);

		cudaMalloc ( (void**) &r_device, taille_mem);

		

		cudaMemcpy( a1_device,A1,taille_mem,cudaMemcpyHostToDevice);

 for(h=0;h<3;h++){

		

		mykernel<<<1,9>>>(a1_device,a2_device,r_device);

		// results are taken back

		cudaMemcpy(R,r_device,taille_mem,cudaMemcpyDeviceToHost);

		//output on the screen

				printf("%f\n",R[0]);

	   // first element of A1 is "marked"

	A1[0]=-1;

	cudaMemcpy( a1_device,A1,taille_mem,cudaMemcpyHostToDevice);

  }

}

results: (if too=&ti(5) is alived between new kernel, 5 is obtained!)

I obtain this:

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

5.000000

code file ishere

So do objects remain between kernels or is it a pure random behaviour? :)

No, automatic variables are not persistent between kernels. Just change [font=“Courier New”]A1[0]=-1;[/font] to [font=“Courier New”]A1[0]=0;[/font] and you will notice yourself.

BTW, the kernel that you included in your post won’t even compile. The one on the external download site does, but there is no need to post links to dubious external download sites, you can attach files directly to your forum post using the upload manager below the message composer.

No, automatic variables are not persistent between kernels. Just change [font=“Courier New”]A1[0]=-1;[/font] to [font=“Courier New”]A1[0]=0;[/font] and you will notice yourself.

BTW, the kernel that you included in your post won’t even compile. The one on the external download site does, but there is no need to post links to dubious external download sites, you can attach files directly to your forum post using the upload manager below the message composer.