Skip to content
Snippets Groups Projects
Commit f361582b authored by jstark's avatar jstark
Browse files

Added MPI communication.

parent 433c71f3
No related branches found
No related tags found
No related merge requests found
......@@ -41,7 +41,9 @@ T string_to_type(const std::string & string_to_convert)
template <typename T>
void read_csv_to_vector(const std::string & input_file, openfpm::vector<T> & output_vector, size_t & m, size_t & n)
{
int total_size = 0;
auto & v_cl = create_vcluster();
// File is accessed and read only by one process
if (v_cl.rank() == 0)
{
if(!check_if_file_exists(input_file))
......@@ -62,13 +64,22 @@ void read_csv_to_vector(const std::string & input_file, openfpm::vector<T> & out
{
output_vector.add(string_to_type<T>(element)); // Convert element from string to type T and append to
// output_vector
elems += 1; // Increase n by one for each element read
total_size += 1; // Increase n by one for each element read
}
m += 1; // Increase m by one for each row read
}
if(m >= 1) n = elems / m; // If at least one row present, divide number of elements read by number of rows to get
if(m >= 1) n = total_size / m; // If at least one row present, divide number of elements read by number of rows to get
// the number of columns
}
// Distribute size of the lin. vector to all other processes s.t. their output_vector can be resized accordingly.
MPI_Bcast(&total_size,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(&m,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);
if (v_cl.rank() != 0)
{
output_vector.resize(total_size);
}
// After the output_vector has been resized to correct size, it can receive the content from process 0.
v_cl.Bcast(output_vector, 0);
v_cl.execute();
}
......
......@@ -6,50 +6,71 @@
#include "CSVReader/CSVReader.hpp"
BOOST_AUTO_TEST_SUITE(CSVReaderTestSuite)
BOOST_AUTO_TEST_CASE(csv_reader_int)
BOOST_AUTO_TEST_CASE(csv_reader_int_test)
{
std::cout << "CWD = " << get_cwd() << std::endl;
std::string csv_file = std::string("../../../openfpm_io/test_data/integer.csv");
// Read csv file into vector while linearizing
openfpm::vector<int> v_lin; // Vector to which csv file will be read to
size_t m, n; // Number of rows m and columns n
read_csv_to_vector(csv_file, v_lin, m, n);
BOOST_CHECK(m == 4);
BOOST_CHECK(n == 3);
BOOST_CHECK(m * n == v_lin.size());
for(int i = 0; i < v_lin.size() / n; ++i)
{
BOOST_CHECK( v_lin.get(i * n) == i + 1);
BOOST_CHECK( v_lin.get(i * n + 1) == (i + 1) * 2);
BOOST_CHECK( v_lin.get(i * n + 2) == v_lin.get(i * n) * v_lin.get(i * n + 1));
}
#ifdef OPENFPM_PDATA
std::string csv_file = std::string("openfpm_io/test_data/integer.csv");
#else
std::string csv_file = std::string("test_data/integer.csv");
#endif
std::cout << "CWD = " << get_cwd() << std::endl;
// Read csv file into vector while linearizing
openfpm::vector<int> v_lin; // Vector to which csv file will be read to
size_t m, n; // Number of rows m and columns n
read_csv_to_vector(csv_file, v_lin, m, n);
auto & v_cl = create_vcluster();
std::cout << "My rank is " << v_cl.rank() << std::endl;
auto v_iter = v_lin.getIterator();
while(v_iter.isNext())
{
auto key = v_iter.get();
std::cout << "Element number " << key << " of rank " << v_cl.rank() << " is " << v_lin.get(key) <<
std::endl;
++v_iter;
}
BOOST_CHECK(m == 4);
BOOST_CHECK(n == 3);
BOOST_CHECK(m * n == v_lin.size());
for(int i = 0; i < v_lin.size() / n; ++i)
{
BOOST_CHECK( v_lin.get(i * n) == i + 1);
BOOST_CHECK( v_lin.get(i * n + 1) == (i + 1) * 2);
BOOST_CHECK( v_lin.get(i * n + 2) == v_lin.get(i * n) * v_lin.get(i * n + 1));
}
std::cout << "Rank " << v_cl.rank() << " reaches end of unit test." << std::endl;
}
BOOST_AUTO_TEST_CASE(csv_reader_char)
BOOST_AUTO_TEST_CASE(csv_reader_char_test)
{
// Read csv file into vector while linearizing
const std::string csv_file = "../../../openfpm_io/test_data/char.csv"; // csv file to be read
openfpm::vector<std::string> v_lin; // Vector to which csv file will be read to
size_t m, n; // Number of rows m and columns n
read_csv_to_vector(csv_file, v_lin, m, n);
BOOST_CHECK(m == 5);
BOOST_CHECK(n == 2);
BOOST_CHECK(m * n == v_lin.size());
openfpm::vector<std::string> col1 = {"a", "b", "c", "d", "e"};
openfpm::vector<std::string> col2 = {"antilope", "ballena", "camel", "dackel", "elefant"};
for(int i = 0; i < v_lin.size() / n; ++i)
{
BOOST_CHECK(col1.get(i) == v_lin.get(i * n));
BOOST_CHECK(col2.get(i) == v_lin.get(i * n + 1));
}
#ifdef OPENFPM_PDATA
std::string csv_file = std::string("openfpm_io/test_data/char.csv");
#else
std::string csv_file = std::string("test_data/char.csv");
#endif
// Read csv file into vector while linearizing
openfpm::vector<std::string> v_lin; // Vector to which csv file will be read to
size_t m, n; // Number of rows m and columns n
read_csv_to_vector(csv_file, v_lin, m, n);
BOOST_CHECK(m == 5);
BOOST_CHECK(n == 2);
BOOST_CHECK(m * n == v_lin.size());
openfpm::vector<std::string> col1 = {"a", "b", "c", "d", "e"};
openfpm::vector<std::string> col2 = {"antilope", "ballena", "camel", "dackel", "elefant"};
for(int i = 0; i < v_lin.size() / n; ++i)
{
BOOST_CHECK(col1.get(i) == v_lin.get(i * n));
BOOST_CHECK(col2.get(i) == v_lin.get(i * n + 1));
}
}
BOOST_AUTO_TEST_SUITE_END()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment