Skip to content

Commit

Permalink
Some minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lformaggia committed May 5, 2024
1 parent be9a293 commit 2a62bed
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Examples/src/InPolygon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ of the polygon, in a consistent way. Yet, the code provided here give an idea of

We have implemented two types of algorithms:

1. The first one calculates the dot product between the normal of each polygon edge and the vector connecting the given
1. The first one, working only for convex polygons, calculates the dot product between the normal of each polygon edge and the vector connecting the given
point and a point of the edge. The given point is inside only if all dot products have the same sign
(we have not made any assumption on the orientation of the edge vertices, so we cannot assume an
orientation for the normal).
Expand Down
6 changes: 3 additions & 3 deletions Examples/src/MyMat0/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ BLAS=yes# the default
# change it to suite your needs if yo do not use modules
# module load suitesparse will also load the blas library
RELEASE=yes
#BLAS_LIB_DIR=-L${mkOpenblasLib}
#BLAS_INC_DIR=-I${mkOpenblasInc}
BLAS_LIB_DIR?=-L${mkOpenblasLib}
BLAS_LIB_INC?=-L${mkOpenblasInc}
#BLAS_LIB_DIR=-L/opt/atlas/lib
#BLAS_INC_DIR=-I/opt/atlas/include
#BLAS_INC_DIR=-I/opt/atlas/includ
BLAS_LIB_NAME=openblas # if you use module you have openblas
#BLAS_LIB_NAME=cblas # if you use module you have openblas
BLAS_LIB_NAME=blas # if you use other versions of blas
Expand Down
209 changes: 111 additions & 98 deletions Examples/src/STL/Reduce/mainReduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ randomVector(std::size_t n)
res.emplace_back(uniform_dist(engine));
timer.stop();
std::cout << " Time taken to create vector:" << timer.wallTime()
<< " microsec\n" << std::endl;
<< " microsec\n"
<< std::endl;
return res;
}
int
Expand All @@ -38,33 +39,35 @@ main()
// Example of simple use of a parallel reduce: add all values
Timings::Chrono timer;
{
timer.start();
std::cout << "Sum= "
<< std::reduce(std::execution::par, // execution policy
std::begin(bigVector), // start
std::end(bigVector), // end
0.0, // initial value
std::plus{}); // binary operator (sum, not needed
// since it is the default
std::cout << std::endl;
timer.stop();
std::cout << "Parallel version. Time taken:" << timer.wallTime() << " microsec" << std::endl;
timer.start();
std::cout << "Sum= "
<< std::reduce(std::execution::par, // execution policy
std::begin(bigVector), // start
std::end(bigVector), // end
0.0, // initial value
std::plus{}); // binary operator (sum, not needed
// since it is the default
timer.stop();
std::cout << std::endl;
std::cout << "Parallel version. Time taken:" << timer.wallTime()
<< " microsec" << std::endl;
}
{
timer.start();
std::cout << "Sum= "
<< std::reduce(std::execution::seq, // execution policy
std::begin(bigVector), // start
std::end(bigVector), // end
0.0, // initial value
std::plus{}); // binary operator (sum, not needed
// since it is the default
std::cout << std::endl;
timer.stop();
std::cout << "Sequential version.Time taken:" << timer.wallTime() << " microsec\n" << std::endl;
timer.start();
std::cout << "Sum= "
<< std::reduce(std::execution::seq, // execution policy
std::begin(bigVector), // start
std::end(bigVector), // end
0.0, // initial value
std::plus{}); // binary operator (sum, not needed
// since it is the default
std::cout << std::endl;
timer.stop();
std::cout << "Sequential version.Time taken:" << timer.wallTime()
<< " microsec\n"
<< std::endl;
}


// A more interesting algorithm, transform_reduce. In this form it applies an
// unitary operation to all values in the range and then applies a reduction
// using a binary operation. In this example we compute the 1-norm
Expand All @@ -73,98 +76,108 @@ main()

auto absolute = [](double const &x) { return std::abs(x); };
{
timer.start();
std::cout << "1-Norm= "
<< std::transform_reduce(std::execution::par, // execution policy
std::begin(bigVector), // start
std::end(bigVector), // end
0.0, // initial value
std::plus{}, // binary operator
absolute); // the lambda as unary operator
std::cout << std::endl;
timer.stop();
std::cout << "Parallel version. Time taken:" << timer.wallTime() << " microsec" << std::endl;
timer.start();
std::cout << "1-Norm= "
<< std::transform_reduce(
std::execution::par, // execution policy
std::begin(bigVector), // start
std::end(bigVector), // end
0.0, // initial value
std::plus{}, // binary operator
absolute); // the lambda as unary operator
std::cout << std::endl;
timer.stop();
std::cout << "Parallel version. Time taken:" << timer.wallTime()
<< " microsec" << std::endl;
}
{
timer.start();
std::cout << "1-Norm= "
<< std::transform_reduce(std::execution::seq, // execution policy
std::begin(bigVector), // start
std::end(bigVector), // end
0.0, // initial value
std::plus{}, // binary operator
absolute); // the lambda as unary operator
std::cout << std::endl;
timer.stop();
std::cout << "Sequential version.Time taken:" << timer.wallTime() << " microsec\n" << std::endl;
}
timer.start();
std::cout << "1-Norm= "
<< std::transform_reduce(
std::execution::seq, // execution policy
std::begin(bigVector), // start
std::end(bigVector), // end
0.0, // initial value
std::plus{}, // binary operator
absolute); // the lambda as unary operator
std::cout << std::endl;
timer.stop();
std::cout << "Sequential version.Time taken:" << timer.wallTime()
<< " microsec\n"
<< std::endl;
}

// Another type of transform_reduce: it does the inner product
auto secondVector{randomVector(n)};
{
timer.start();
std::cout << "Dot product= "
<< std::transform_reduce(std::execution::par, // execution policy
std::begin(bigVector), // start first
std::end(bigVector), // end firse
std::begin(secondVector), // begin second
0.0); // Initial Value
std::cout << std::endl;
timer.stop();
std::cout << "Parallel version. Time taken:" << timer.wallTime() << " microsec" << std::endl;
timer.start();
std::cout << "Dot product= "
<< std::transform_reduce(std::execution::par, // execution policy
std::begin(bigVector), // start first
std::end(bigVector), // end firse
std::begin(secondVector), // begin second
0.0); // Initial Value
std::cout << std::endl;
timer.stop();
std::cout << "Parallel version. Time taken:" << timer.wallTime()
<< " microsec" << std::endl;
}
{
timer.start();
std::cout << "Dot product= "
<< std::transform_reduce(std::execution::seq, // execution policy
std::begin(bigVector), // start first
std::end(bigVector), // end firse
std::begin(secondVector), // begin second
0.0); // Initial Value
std::cout << std::endl;
timer.stop();
std::cout << "Sequential version.Time taken:" << timer.wallTime() << " microsec\n" << std::endl;
} // This is the most complex form.
timer.start();
std::cout << "Dot product= "
<< std::transform_reduce(std::execution::seq, // execution policy
std::begin(bigVector), // start first
std::end(bigVector), // end firse
std::begin(secondVector), // begin second
0.0); // Initial Value
std::cout << std::endl;
timer.stop();
std::cout << "Sequential version.Time taken:" << timer.wallTime()
<< " microsec\n"
<< std::endl;
} // This is the most complex form.
// It applies a binary operation to all couple of elements of the two ranges
// then a reduction applying a second binary operation consecutively on the
// result. For instance here we compute the 1-norm of the difference ||A-B||_1
auto absDiff = [](const double &x, const double &y) {
return std::abs(x - y);
};
{
timer.start();
std::cout
<< "|a-b|_1= "
<< std::transform_reduce(
std::execution::par, // execution policy
std::begin(bigVector), // start first
std::end(bigVector), // end firse
std::begin(secondVector), // begin second
0.0, // Initial Value
std::plus{}, // binary operation performd for reduction
absDiff // binary operation performed on each couple of elements
);
timer.stop();
timer.start();
std::cout
<< "|a-b|_1= "
<< std::transform_reduce(
std::execution::par, // execution policy
std::begin(bigVector), // start first
std::end(bigVector), // end firse
std::begin(secondVector), // begin second
0.0, // Initial Value
std::plus{}, // binary operation performd for reduction
absDiff // binary operation performed on each couple of elements
);
timer.stop();

std::cout << std::endl;
std::cout << "Parallel version. Time taken:" << timer.wallTime() << " microsec" << std::endl;
std::cout << std::endl;
std::cout << "Parallel version. Time taken:" << timer.wallTime()
<< " microsec" << std::endl;
}
{
timer.start();
std::cout
<< "|a-b|_1= "
<< std::transform_reduce(
std::execution::seq, // execution policy
std::begin(bigVector), // start first
std::end(bigVector), // end firse
std::begin(secondVector), // begin second
0.0, // Initial Value
std::plus{}, // binary operation performd for reduction
absDiff // binary operation performed on each couple of elements
);
timer.stop();
timer.start();
std::cout
<< "|a-b|_1= "
<< std::transform_reduce(
std::execution::seq, // execution policy
std::begin(bigVector), // start first
std::end(bigVector), // end firse
std::begin(secondVector), // begin second
0.0, // Initial Value
std::plus{}, // binary operation performd for reduction
absDiff // binary operation performed on each couple of elements
);
timer.stop();

std::cout << std::endl;
std::cout << "Sequential version. Time taken:" << timer.wallTime() << " microsec" << std::endl;
std::cout << std::endl;
std::cout << "Sequential version. Time taken:" << timer.wallTime()
<< " microsec" << std::endl;
}
}

0 comments on commit 2a62bed

Please sign in to comment.