PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
testParallelComp.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include <PeriDEMConfig.h>
12#include "testParallelCompLib.h"
13#include "util/io.h"
14#include "util/parallelUtil.h"
15#include <fmt/format.h>
16#include <iostream>
17
18int main(int argc, char *argv[]) {
19
20 // init parallel
21 util::parallel::initMpi(argc, argv);
22 int mpiSize = util::parallel::mpiSize(), mpiRank = util::parallel::mpiRank();
23 util::io::print(fmt::format("Initialized MPI. MPI size = {}, MPI rank = {}\n", mpiSize, mpiRank));
25
26 // init logger
28
29 // parse input arguments
30 util::io::InputParser input(argc, argv);
31
32 if (input.cmdOptionExists("-h") or !input.cmdOptionExists("-o")) {
33 // print help
34 std::cout << argv[0] << " (Version " << MAJOR_VERSION << "."
35 << MINOR_VERSION << "." << UPDATE_VERSION
36 << ") -o <test-option; 0 - taskflow, 1 - parallel on in-built mesh, 2 - user-defined mesh>"
37 " -i <vector-size> -n <grid-size>"
38 " -m <horizon-integer-factor>"
39 " -nThreads <number of threads to be used in taskflow>" << std::endl;
40 std::cout << "To test taskflow, run\n";
41 std::cout << argv[0] << " -o 0 -i 10000"
42 " -nThreads <number of threads to be used in taskflow>\n";
43 std::cout << "To test parallel using in-built mesh, run\n";
44 std::cout << argv[0] << " -o 1 -m 4 -n 50\n";
45 std::cout << "To test parallel on user-provided mesh (filename = filepath/meshfile.vtu)" << std::endl;
46 std::cout << argv[0] << " -o 2 -m 4 -f filepath/meshfile.vtu" << std::endl;
47 exit(EXIT_FAILURE);
48 }
49
50 // read input file
51 size_t testOption(-1);
52
53 if (input.cmdOptionExists("-o")) testOption = std::stoi(input.getCmdOption("-o"));
54
55 // test
56 if (testOption == 0) {
57 util::io::print("\nTesting taskflow\n\n");
58
59 size_t nTaskflow;
60 if (input.cmdOptionExists("-i")) nTaskflow = std::stoi(input.getCmdOption("-i"));
61 else {
62 nTaskflow = 100000;
63 util::io::print(fmt::format("Running test with default vector-size = {}\n", nTaskflow));
64 }
65
66 unsigned int nThreads;
67 if (input.cmdOptionExists("-nThreads")) nThreads = std::stoi(input.getCmdOption("-nThreads"));
68 else {
69 nThreads = std::thread::hardware_concurrency();
70 util::io::print(fmt::format("Running test with default number of threads = {}\n", nThreads));
71 }
72 // set number of threads
74 util::io::print(fmt::format("Number of threads = {}\n", util::parallel::getNThreads()));
75
76 std::vector<size_t> N_test = {size_t(nTaskflow/100), size_t(nTaskflow/10), nTaskflow, 10*nTaskflow, 100*nTaskflow};
77 int seed = 0;
78 size_t test_count = 0;
79 for (auto n : N_test) {
80 util::io::print(fmt::format("**** Test number = {} ****\n"
81 "Test parameters: N = {}\n\n",
82 test_count++, n));
83 auto msg = test::testTaskflow(n, seed);
84 util::io::print(msg);
85 }
86 } else if (testOption == 1 or testOption == 2) {
87
88 util::io::print("\nTesting MPI parallelization\n\n");
89
90 if (mpiSize < 2) {
91 std::cout << "\nNo tests for mpiSize = 1. Skipping this test.\n\n";
92 } else {
93 // read arguments
94 size_t nGrid(0), mHorizon;
95 std::string meshFilename("");
96
97 if (input.cmdOptionExists("-n"))
98 nGrid = size_t(std::stoi(input.getCmdOption("-n")));
99 else {
100 // set only if we are running test using in-built mesh
101 if (testOption == 1) {
102 nGrid = 50;
103 util::io::print(fmt::format("Running test with default grid size = {}\n", nGrid));
104 }
105 }
106
107 if (input.cmdOptionExists("-m"))
108 mHorizon = size_t(std::stoi(input.getCmdOption("-m")));
109 else {
110 mHorizon = 4;
111 util::io::print(fmt::format("Running test with default integer factor for horizon = {}\n", mHorizon));
112 }
113
114 // read filename
115 if (input.cmdOptionExists("-f")) meshFilename = input.getCmdOption("-f");
116
117 // check if nGrid and meshFilename are compatible
118 if ((nGrid > 0 and testOption == 2) or
119 (!meshFilename.empty() and testOption == 1)) {
120 std::cerr
121 << "Please specify either using uniform mesh (in-built) or user-defined mesh "
122 "to perform the MPI test. "
123 "That is, either specify '-o 1 -n <grid-size>' "
124 "or '-o 2 -f <mesh-filename>'.\n";
125 exit(1);
126 }
127
128 // call test function
129 test::testMPI(nGrid, mHorizon, testOption, meshFilename);
130 }
131 } else {
132 util::io::print("Invalid option -o argument.\n");
133 }
134
135 MPI_Finalize();
136 return EXIT_SUCCESS;
137}
size_t const MINOR_VERSION
size_t const UPDATE_VERSION
size_t const MAJOR_VERSION
Input command line argument parser.
Definition io.h:355
bool cmdOptionExists(const std::string &option) const
Check if argument exists.
Definition io.h:387
const std::string & getCmdOption(const std::string &option) const
Get value of argument specified by key.
Definition io.h:372
std::string testTaskflow(size_t N, int seed)
Perform test on taskflow.
void testMPI(size_t nGrid=10, size_t mHorizon=3, size_t testOption=0, std::string meshFilename="")
Perform parallelization test using MPI on mesh partition based on metis.
void print(const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted information.
Definition io.h:108
void initLogger(int debug_level=logger_default_debug_lvl, std::string filename="")
Initializes the logger.
Definition io.cpp:15
unsigned int getNThreads()
Get number of threads to be used by taskflow.
void initNThreads(unsigned int nThreads=std::thread::hardware_concurrency())
Initializes MpiStatus struct.
const MpiStatus * getMpiStatus()
Returns pointer to MpiStatus struct.
void initMpi(int argc=0, char *argv[]=nullptr)
Initializes MPI and also creates MpiStatus struct.
int mpiSize()
Get size (number) of processors.
int mpiRank()
get rank (id) of this processor
int main()