SHA256
fix measurement func
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
CompileFlags:
|
CompileFlags:
|
||||||
Add: [./include/num_threads.cpp -std=c++20, -fopenmp]
|
Add: [-std=c++20, -fopenmp]
|
||||||
|
|||||||
+52
-44
@@ -1,55 +1,63 @@
|
|||||||
#include <ostream>
|
|
||||||
#include <thread>
|
|
||||||
#include <omp.h>
|
|
||||||
#include <thread>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <vector>
|
|
||||||
#include <concepts>
|
#include <concepts>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <omp.h>
|
||||||
|
#include <ostream>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
unsigned g_threads = std::thread::hardware_concurrency();
|
unsigned g_threads = std::thread::hardware_concurrency();
|
||||||
const unsigned max_threads = g_threads;
|
const unsigned max_threads = g_threads;
|
||||||
|
|
||||||
unsigned get_num_threads(){
|
unsigned get_num_threads() { return g_threads; }
|
||||||
return g_threads;
|
|
||||||
|
void set_num_threads(const unsigned T) {
|
||||||
|
if (T == 0 || T > max_threads)
|
||||||
|
return;
|
||||||
|
g_threads = T;
|
||||||
|
omp_set_num_threads(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_num_threads(const unsigned T){
|
struct measurement {
|
||||||
if(T==0 || T > max_threads)
|
unsigned T;
|
||||||
return ;
|
double time;
|
||||||
g_threads = T;
|
unsigned result;
|
||||||
omp_set_num_threads(T);
|
double speedup;
|
||||||
}
|
|
||||||
|
|
||||||
struct measurement{
|
|
||||||
unsigned T;
|
|
||||||
double time;
|
|
||||||
double speedup;
|
|
||||||
unsigned result;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::invocable<const unsigned* , size_t> f >
|
template <std::invocable<const unsigned *, size_t> F>
|
||||||
std::vector<measurement> measure_time (f, const unsigned* V, size_t n)
|
std::vector<measurement> measure_time(F f, const unsigned *V, size_t n) {
|
||||||
{
|
std::vector<measurement> ret;
|
||||||
std::vector<measurement> ret;
|
const unsigned max_threads = get_num_threads();
|
||||||
const unsigned max_threads = gen_num_threads();
|
for (unsigned T = 1; T <= max_threads; T++) {
|
||||||
for(unsigned T = 1;T<=max_threads();T++){
|
set_num_threads(T);
|
||||||
set_num_threads(T);
|
double t0 = omp_get_wtime();
|
||||||
double t0 = omp_get_wtime();
|
const unsigned result = f(V, n);
|
||||||
const unsigned result = f(V, n)
|
double t1 = omp_get_wtime();
|
||||||
double t1 = omp_get_wtime();
|
ret.push_back(measurement{
|
||||||
ret.push_back(measure{
|
.T = T,
|
||||||
.T = T,
|
.time = t1 - t0,
|
||||||
.time = t1-t0,
|
.result = result,
|
||||||
.result = result,
|
.speedup = ret.size() > 0 ? ret.back().time / ret[0].time : 1});
|
||||||
.speedup = ret.size()>0? ret.back()/ret[0] : 1
|
}
|
||||||
});
|
set_num_threads(max_threads);
|
||||||
}
|
return ret;
|
||||||
sen_num_threads(max_threads);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void to_csv(std::ostream& os, const std::vector<measurement> & m ){
|
void to_csv(std::ostream &os, const std::vector<measurement> &m) {
|
||||||
os << "T, time, speedups, result, \n";
|
os << "T, time, speedups, result, \n";
|
||||||
for ( auto i : m){
|
for (auto i : m) {
|
||||||
os << i.T<<", "<<i.time<<", "<<i.speedup<<", "<<i.result<<"\n";
|
os << i.T << ", " << i.time << ", " << i.speedup << ", " << i.result
|
||||||
}
|
<< "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::invocable<const unsigned *, size_t> F>
|
||||||
|
void measure_time(F f, const unsigned *V, size_t n, std::string name) {
|
||||||
|
std::filesystem::create_directory(std::string("measurements/") + name +
|
||||||
|
".csv");
|
||||||
|
std::filebuf fb;
|
||||||
|
fb.open("test.txt", std::ios::out);
|
||||||
|
std::ostream os(&fb);
|
||||||
|
to_csv(os, measure_time(f, V, n));
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -1,3 +1,4 @@
|
|||||||
|
#include "../include/num_thread.cpp"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
@@ -7,7 +8,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "../include/num_thread.cpp"
|
|
||||||
#define CHACHE_LINE 64
|
#define CHACHE_LINE 64
|
||||||
|
|
||||||
// par - parallel
|
// par - parallel
|
||||||
@@ -115,7 +115,6 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
std::size_t n = 1u << 28;
|
std::size_t n = 1u << 28;
|
||||||
auto data = std::make_unique<unsigned[]>(n);
|
auto data = std::make_unique<unsigned[]>(n);
|
||||||
|
|||||||
Reference in New Issue
Block a user