Commit 84c58c55 authored by jstark's avatar jstark
Browse files

Merged CSVReader unit test and corr. test data from master.

parent 03d72a92
Pipeline #4619 passed with stages
in 8 minutes and 14 seconds
......@@ -40,12 +40,15 @@ set(HDF5_PREFER_PARALLEL TRUE)
set (ENV{BOOST_ROOT} ${BOOST_ROOT})
set (Boost_NO_BOOST_CMAKE OFF)
find_package(Boost 1.72.0 REQUIRED COMPONENTS unit_test_framework iostreams program_options OPTIONAL_COMPONENTS fiber context)
find_package(Boost 1.72.0 REQUIRED COMPONENTS unit_test_framework iostreams program_options system filesystem
OPTIONAL_COMPONENTS fiber context)
find_package(MPI REQUIRED)
find_package(HDF5 REQUIRED)
find_package(TinyObjLoader)
find_package(PETSc)
find_package(OpenMP)
if (NOT CUDA_ON_BACKEND STREQUAL "HIP")
find_package(OpenMP)
endif()
if (CUDA_ON_BACKEND STREQUAL "HIP" AND NOT HIP_FOUND)
find_package(HIP)
endif()
......
......@@ -10,6 +10,8 @@
#include <string>
#include "Vector/map_vector.hpp"
#include "util/PathsAndFiles.hpp"
#include "VCluster/VCluster.hpp"
/**@brief Converts string into template type T.
*
......@@ -27,7 +29,8 @@ T string_to_type(const std::string & string_to_convert)
}
/**@brief Reads csv file and saves elements as linearized vector.
/**@brief Csv file is read by process rank 0 and the elements are saved into a linearized vector. The vector is
* broadcasted to the other processes.
*
* @tparam T Type that elements from csv file shall be converted to.
* @param input_file std::string containing the path to the input csv file.
......@@ -38,24 +41,47 @@ 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)
{
std::ifstream file(input_file); // Create file pointer and open file
std::string line, element;
size_t total_size = 0;
m = 0;
n = 0;
while (getline(file, line)) // Read entire row and store as one string in line
auto & v_cl = create_vcluster();
// File is accessed and read only by one process
if (v_cl.rank() == 0)
{
std::stringstream stream_line(line); // Needed to break line into elements later one
while (getline(stream_line, element, ',')) // Read every column of line and store content as string in element
if(!check_if_file_exists(input_file))
{
std::cout << "Cannot find < " << input_file << " >. Please check path specification. Aborting..."
<< std::endl;
abort();
}
std::ifstream file(input_file); // Create file pointer and open file
std::string line, element;
while (getline(file, line)) // Read entire row and store as one string in line
{
output_vector.add(string_to_type<T>(element)); // Convert element from string to type T and append to
// output_vector
n += 1; // Increase n by one for each element read
std::stringstream stream_line(line); // Needed to break line into elements later one
while (getline(stream_line, element, ',')) // Read every column of line and store content as string in element
{
output_vector.add(string_to_type<T>(element)); // Convert element from string to type T and append to
// output_vector
total_size += 1; // Increase n by one for each element read
}
m += 1; // Increase m by one for each row read
}
m += 1; // Increase m by one for each row read
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_UNSIGNED,0,MPI_COMM_WORLD);
MPI_Bcast(&m,1,MPI_UNSIGNED,0,MPI_COMM_WORLD);
MPI_Bcast(&n,1,MPI_UNSIGNED,0,MPI_COMM_WORLD);
if (v_cl.rank() != 0)
{
output_vector.resize(total_size);
}
n /= m; // Divide number of elements read by number of rows to get number of columns
// 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();
}
#endif //OPENFPM_IO_CSVREADER_HPP
//
// Created by jstark on 28.12.21.
//
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "CSVReader/CSVReader.hpp"
BOOST_AUTO_TEST_SUITE(CSVReaderTestSuite)
BOOST_AUTO_TEST_CASE(csv_reader_int_test)
{
#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
// 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));
}
}
BOOST_AUTO_TEST_CASE(csv_reader_char_test)
{
#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()
a,antilope
b,ballena
c,camel
d,dackel
e,elefant
\ No newline at end of file
1,2,2
2,4,8
3,6,18
4,8,32
\ No newline at end of file
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