11.2.4.2 : Le fichier main_create_file.cpp



1
2
3
4
5
6
#include <iostream>
#include <cufft.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <curand.h>
#include <sstream>


Les includes de Thrust pour manipuler des vecteurs sur le CPU et le GPU
1
2
3
4
5
6
7
8
9
10
11
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>

#include "RawHdf5_hdf5.h"

///Initialised the signal of the data with a random normal distribution
/**	@param[out] data : Data to be initialised
*/
void initialisationSignalWithRandomNormal(RawHdf5 & data){
	int nbEvent = data.getNbEntries();
	std::cout << "initialisationSignalWithRandomNormal : allocate signal on GPU with "<<nbEvent<<" events" << std::endl;


Pour une raison obscure, thrust ne veut pas m'allouer mon vecteur directemenr sur le GPU, donc, on l'alloue vite fait sur le CPU et on le copie :
1
2
3
4
5
6
	thrust::host_vector<float> hSignal(nbEvent*RAWHDF5_SIGNAL_0);
	thrust::device_vector<float> dSignal = hSignal;
	std::cout << "initialisationSignalWithRandomNormal : initlialise the CUDA random generator" << std::endl;
	curandGenerator_t generator;
	curandCreateGenerator(&generator, CURAND_RNG_PSEUDO_DEFAULT);			//Create the generator
	curandSetPseudoRandomGeneratorSeed(generator, 42);				//Set the random seed


On génére une distribution gaussienne. Je dois dire que le dSignal.data().get() ressemble à une grosse bidouille, mais c'est l'API thrust qui est comme ça :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
	std::cout << "initialisationSignalWithRandomNormal : generate random numbers" << std::endl;
	curandGenerateNormal(generator, (float*)dSignal.data().get(), RAWHDF5_SIGNAL_0*nbEvent, 2.0f, 1.0f);	//Generate a normal distribution (generator, data, size, mean, std)
	curandDestroyGenerator(generator);						//Destroy the generator
	
	std::cout << "initialisationSignalWithRandomNormal : getting back data from the GPU" << std::endl;
	thrust::copy(dSignal.begin(), dSignal.end(), data.getSignalFull());
}

///Create a HDF5 file with a Random Normal distribution
/**	@param baseOutputFileName : base name of the output file
 * 	@param nbEvent : number of event to be generated
*/
void createRandomNormalFile(const std::string & baseOutputFileName, int nbEvent){
	RawHdf5H5 hfile;
	hfile.resize(nbEvent);
	
// 	RawHdf5 data;
// 	hfile.clone(data);	//Let's link the file data without copying it
	
	initialisationSignalWithRandomNormal(hfile);
	
	std::stringstream fileName;
	fileName << baseOutputFileName << "_" << nbEvent << ".h5";
	
	hfile.write(fileName.str());
	std::cout << "Done" << std::endl;
}

int main(int argc, char** argv){
	int nbEvent = 2048000;
	if(argc < 2 || argc > 4){
		std::cout << "Usage :" << std::endl;
		std::cout << "\tcreate_hdf5_normal_distribution output" << std::endl;
		std::cout << "\tcreate_hdf5_normal_distribution output 2048000" << std::endl;
		std::cout << "Parameters :" << std::endl;
		std::cout << "\tHDF5 output file to be created" << std::endl;
		std::cout << "\tNumber of events to be generated" << std::endl;
		return 0;
	}
	std::string baseOutputFileName(argv[1]);
	if(argc >= 3){
		nbEvent = atoi(argv[2]);
	}
	createRandomNormalFile(baseOutputFileName, nbEvent);
	return 0;
}


Le main_create_file.cpp complet :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

#include <iostream>
#include <cufft.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <curand.h>
#include <sstream>

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>

#include "RawHdf5_hdf5.h"

///Initialised the signal of the data with a random normal distribution
/**	@param[out] data : Data to be initialised
*/
void initialisationSignalWithRandomNormal(RawHdf5 & data){
	int nbEvent = data.getNbEntries();
	std::cout << "initialisationSignalWithRandomNormal : allocate signal on GPU with "<<nbEvent<<" events" << std::endl;
	thrust::host_vector<float> hSignal(nbEvent*RAWHDF5_SIGNAL_0);
	thrust::device_vector<float> dSignal = hSignal;
	std::cout << "initialisationSignalWithRandomNormal : initlialise the CUDA random generator" << std::endl;
	curandGenerator_t generator;
	curandCreateGenerator(&generator, CURAND_RNG_PSEUDO_DEFAULT);			//Create the generator
	curandSetPseudoRandomGeneratorSeed(generator, 42);				//Set the random seed
	std::cout << "initialisationSignalWithRandomNormal : generate random numbers" << std::endl;
	curandGenerateNormal(generator, (float*)dSignal.data().get(), RAWHDF5_SIGNAL_0*nbEvent, 2.0f, 1.0f);	//Generate a normal distribution (generator, data, size, mean, std)
	curandDestroyGenerator(generator);						//Destroy the generator
	
	std::cout << "initialisationSignalWithRandomNormal : getting back data from the GPU" << std::endl;
	thrust::copy(dSignal.begin(), dSignal.end(), data.getSignalFull());
}

///Create a HDF5 file with a Random Normal distribution
/**	@param baseOutputFileName : base name of the output file
 * 	@param nbEvent : number of event to be generated
*/
void createRandomNormalFile(const std::string & baseOutputFileName, int nbEvent){
	RawHdf5H5 hfile;
	hfile.resize(nbEvent);
	
// 	RawHdf5 data;
// 	hfile.clone(data);	//Let's link the file data without copying it
	
	initialisationSignalWithRandomNormal(hfile);
	
	std::stringstream fileName;
	fileName << baseOutputFileName << "_" << nbEvent << ".h5";
	
	hfile.write(fileName.str());
	std::cout << "Done" << std::endl;
}

int main(int argc, char** argv){
	int nbEvent = 2048000;
	if(argc < 2 || argc > 4){
		std::cout << "Usage :" << std::endl;
		std::cout << "\tcreate_hdf5_normal_distribution output" << std::endl;
		std::cout << "\tcreate_hdf5_normal_distribution output 2048000" << std::endl;
		std::cout << "Parameters :" << std::endl;
		std::cout << "\tHDF5 output file to be created" << std::endl;
		std::cout << "\tNumber of events to be generated" << std::endl;
		return 0;
	}
	std::string baseOutputFileName(argv[1]);
	if(argc >= 3){
		nbEvent = atoi(argv[2]);
	}
	createRandomNormalFile(baseOutputFileName, nbEvent);
	return 0;
}
Le fichier main_create_file.cpp est disponible ici.