4.1.8.1.3 : La fonction générale du programme


Maintenant, implémentons la fonctions générale :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
///Simulate the images
/**	@param nbRow : number of rows of the images to be created
 * 	@param nbCol : number of columns of the images to be created
 * 	@param nbImage : number of images to be created
 * 	@param nbExtraStep : number of extra steps to be computed between images
 * 	@param killRate : rate of the process which converts V into P
 * 	@param feedRate : rate of the process which feeds U and drains U, V and P
 * 	@param dt : time interval between two computation
 * 	@param outputFile : name of the file to be created
 * 	@return true on succsess, false otherwise
*/
bool simulateImage(size_t nbRow, size_t nbCol, size_t nbImage, size_t nbExtraStep, float killRate, float feedRate, float dt, const std::string & outputFile){
#ifdef SELECTED_GPU
	int deviceId = asterics_setDevice(SELECTED_GPU);
#else
	int deviceId(0);
#endif
	int deviceCount(asterics_getNbCudaDevice());
	size_t maxNbThreadPerBlockX(0), maxNbBlockX(0), totalGpuMemory(0);
	asterics_getGpuInfoMem(maxNbThreadPerBlockX, maxNbBlockX, totalGpuMemory, deviceId);
	std::cout << "simulateImage : found " << deviceCount << " device(s)" << std::endl;
	


Nous allons commencer par indiquer quelques paramètres utiles pour l'utilisateur :

1
	std::cout << "simulateImage : nbRow = " << nbRow << ", nbCol = " << nbCol << std::endl;


Ensuite, nous devons définir l'ensemble de nos images en HDF5 :

1
2
3
	MatrixHdf5 fullMat;
	fullMat.setAllDim(nbCol, nbRow);
	fullMat.resize(nbImage);


On créé et on initialise les PTensor et les pointeurs de nos images temporaires :

1
2
3
4
5
	size_t paddedNbRow(nbRow + 2l), paddedNbCol(nbCol + 2l);
	
	PTensor<float> tmpInU, tmpInV, tmpOutU, tmpOutV;
	float *tmpU1 = NULL, *tmpU2 = NULL, *tmpV1 = NULL, *tmpV2 = NULL;
	allocate_temporary(tmpU1, tmpU2, tmpV1, tmpV2, tmpInU, tmpInV, tmpOutU, tmpOutV, paddedNbRow, paddedNbCol);


On définit notre stencil 3x3 (avec éventuellement des coéfficients plus sérieux mais qui donnent des résultats moins rigolos) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	long nbStencilRow(3l), nbStencilCol(3l);
	
	float diffusionRateU(0.1f), diffusionRateV(0.05f);
	//This matrix of neigbour exchange is quite accurate but gives not so fun results
// 	float matDeltaSquare[] = 	{0.05f, 0.2f, 0.05f,
// 					0.2f, 0.0f, 0.2f,
// 					0.05f, 0.2f, 0.05f};
	float matDeltaSquare[] = 	{1.0f, 1.0f, 1.0f,
					1.0f, 1.0f, 1.0f,
					1.0f, 1.0f, 1.0f};
	
	
	float * matOutV = fullMat.getTabMatFull();
	gray_scott_cuda(matOutV, tmpU1, tmpV1,
			nbImage, nbExtraStep, nbRow, nbCol, 
			paddedNbRow, paddedNbCol,
			matDeltaSquare, nbStencilRow, nbStencilCol,
			diffusionRateU, diffusionRateV, feedRate, killRate, dt,
			maxNbThreadPerBlockX, maxNbBlockX, totalGpuMemory);


On dit que le calcul est fini :

1
	std::cerr << "Done" << std::endl;


On écrit le fichier HDF5 :

1
2
	//Let's save the output file
	fullMat.write(outputFile);


On renvoie true car tout s'est bien passé et on finit la fonction :

1
2
	return true;
}