Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void runMatrixExperimentOMP(unsigned int min,
- unsigned int max,
- unsigned int threads,
- unsigned int repeats,
- std::string file_out) {
- std::cout << "Matrix multipication OpenMP benchmark." << std::endl;
- // Attempt to open the file for writing
- std::ofstream fos(file_out);
- if (!fos.good()) {
- std::cerr << "Could not open file " + file_out << std::endl;
- std::exit(-1);
- }
- // Generate a stringstream to write output to both stdout and a file
- std::stringstream ss;
- // Create a timer used for wall-clock time measurements
- Timer t;
- // Dump a header
- generateHeader(ss, {"Experiment", "Matrix size", "Construct (s)", "Randomize (s)"}, {"Float", "Double"}, repeats);
- dump(ss, fos, std::cout);
- // Iterate over each experiment
- for (unsigned int e = min; e < max; e++) {
- auto mat_dim = 1ul << e;
- // Print experiment number
- ss << std::setw(15) << (std::to_string(e) + ",");
- // Print the problem size
- ss << std::setw(15) << (std::to_string(mat_dim) + ",") << std::flush;
- // Create the matrices
- t.start(); // Start the timer.
- auto a = Matrix<float>(mat_dim, mat_dim); // Make a matrix
- auto b = Matrix<float>(mat_dim, mat_dim); // And another one, transposed.
- auto c = Matrix<double>(mat_dim, mat_dim); // And another one, doubles.
- auto d = Matrix<double>(mat_dim, mat_dim); // And another one, transposed.
- t.stop(); // Stop the timer.
- t.report(ss); // Put interval on stdout
- // Randomize their contents
- t.start();
- a.randomize();
- b.randomize();
- c.randomize();
- d.randomize();
- t.stop();
- t.report(ss);
- // Dump the initialization output
- dump(ss, fos, std::cout);
- for(unsigned int i = 0; i < repeats; i++){
- t.start(); // Start the timer.
- auto result = Matrix<float>(a.rows,b.columns);
- // Multiply the matrices
- result = multiplyMatricesOMP(a, b, threads);
- //result.print();
- t.stop(); // Stop the timer.
- t.report(ss);
- // Dump the repeat outcome
- dump(ss,fos,std::cout);
- }
- for(unsigned int i = 0; i < repeats; i++){
- t.start(); // Start the timer.
- auto result = Matrix<double>(c.rows, d.columns);
- // Multiply the matrices
- result = multiplyMatricesOMP(c, d, threads);
- //result.print();
- t.stop();
- t.report(ss, i+1 == repeats);
- // Dump the initialization output
- dump(ss,fos,std::cout);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement