Commit 907675e0 authored by CHAMONT David's avatar CHAMONT David
Browse files

Better exercice !

parent 40d2cd71
......@@ -1006,12 +1006,15 @@
"source": [
"# Exercice\n",
"\n",
"Rather than looking at each isolated code line, it is often profitable to take a look at the big picture : how to simply move one part of the computation, so to get exactly the same output, a lot faster ?"
"Let's assume that we always run the program below with a `degree`which is even.\n",
"1. Replace `val *= data` with `val *= data*data`, and `j<degree` with `j<(degree/2)`. What is the effect on the execution time ?\n",
"2. Ask for `std::cout.precision(18)`, and compare the original and the previous implementation. Why the result is slightly differing ?\n",
"3. Try to compile with `-Ofast`."
]
},
{
"cell_type": "code",
"execution_count": 521,
"execution_count": 536,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -1029,50 +1032,50 @@
"source": [
"%%file tmp.operations.cpp\n",
"\n",
"#include <valarray>\n",
"#include <cstdlib>\n",
"#include <array>\n",
"\n",
"int const SIZE = 1024 ;\n",
"int const REPEAT = 100000 ;\n",
"\n",
"using Array = std::array<double,SIZE> ;\n",
"#include <cassert>\n",
"#include <iostream>\n",
"\n",
"void randomize( Array & x ) {\n",
" srand(1) ;\n",
" for ( int i=0 ; i<SIZE ; ++i )\n",
" x[i] = (std::rand()/(RAND_MAX+1.)-0.5) ;\n",
"}\n",
"std::valarray<double> generate( int size )\n",
" {\n",
" std::valarray<double> datas(size) ;\n",
" for ( double & data : datas )\n",
" { data = std::rand()/(RAND_MAX+1.) ; }\n",
" return datas ;\n",
" }\n",
"\n",
"void multiply( Array const & x, Array & y ) {\n",
" for ( int r=0 ; r<REPEAT ; ++r )\n",
" for ( int i=0 ; i<SIZE ; ++i )\n",
" y[i] += x[i]/10. ;\n",
"}\n",
" \n",
"void reduce( Array const & y ) {\n",
" double res {0.} ;\n",
" for ( int i=0 ; i<SIZE ; ++i ) {\n",
" res += y[i] ;\n",
"double pow_reduce( std::valarray<double> const & datas, int degree )\n",
" {\n",
" double res = 0 ;\n",
" for ( double data : datas )\n",
" {\n",
" double val = 1 ; \n",
" for ( int j=0 ; j<degree ; ++j )\n",
" val *= data ;\n",
" res += val ;\n",
" }\n",
" return res ;\n",
" }\n",
" std::cout<<(res/SIZE)<<std::endl ;\n",
"}\n",
"\n",
"int main()\n",
"int main( int argc, char * argv[] )\n",
" {\n",
" Array x, y ;\n",
" randomize(x) ;\n",
" y.fill(0.) ;\n",
" multiply(x,y) ;\n",
" reduce(y) ;\n",
" assert(argc==3) ;\n",
" int size {atoi(argv[1])} ;\n",
" int degree {atoi(argv[2])} ;\n",
"\n",
" auto datas = generate(size) ;\n",
" auto res = pow_reduce(datas,degree) ;\n",
" std::cout << res << std::endl ;\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 522,
"execution_count": 545,
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "skip"
}
},
"outputs": [
......@@ -1088,24 +1091,16 @@
"%%file tmp.operations.sh\n",
"\n",
"rm -f tmp.operations.exe\n",
"g++ -std=c++17 -O1 tmp.operations.cpp -o tmp.operations.exe\n",
"./tmp.operations.exe $*\n",
"\n",
"rm -f tmp.operations.py\n",
"echo \"t = 0\" >> tmp.operations.py\n",
"for i in 0 1 2 3 4 5 6 7 8 9\n",
"do \\time -f \"t += %U\" -a -o ./tmp.operations.py ./tmp.operations.exe $* >> /dev/null\n",
"done\n",
"echo \"print('(~ {:.3f} s)'.format(t/10.))\" >> tmp.operations.py\n",
"python3 tmp.operations.py"
"g++ -std=c++17 -O3 tmp.operations.cpp -o tmp.operations.exe\n",
"time ./tmp.operations.exe $*"
]
},
{
"cell_type": "code",
"execution_count": 523,
"execution_count": 546,
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "skip"
}
},
"outputs": [
......@@ -1113,13 +1108,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
"67.5054\n",
"(~ 0.142 s)\n"
"0.656155\r\n",
"\r\n",
"real\t0m1.767s\r\n",
"user\t0m1.767s\r\n",
"sys\t0m0.000s\r\n"
]
}
],
"source": [
"!bash -l ./tmp.operations.sh"
"!bash -l ./tmp.operations.sh 1024 65536"
]
},
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment