LCOV - code coverage report
Current view: top level - test - STLAlgorithmTest.cc (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 65 65 100.0 %
Date: 2016-06-29 12:30:40 Functions: 17 17 100.0 %

          Line data    Source code
       1             : #include <libdash.h>
       2             : #include <gtest/gtest.h>
       3             : #include "TestBase.h"
       4             : #include "STLAlgorithmTest.h"
       5             : 
       6             : #include <algorithm>
       7             : #include <vector>
       8             : #include <utility>
       9             : 
      10             : namespace std {
      11             :   template <class T1, class T2>
      12             :   std::ostream & operator<<(
      13             :     std::ostream & os,
      14             :     const std::pair<T1, T2> & p) {
      15             :     os << "(" << p.first << "," << p.second << ")";
      16             :     return os;
      17             :   }
      18             : }
      19             : 
      20          20 : TEST_F(STLAlgorithmTest, StdCopyGlobalToLocal) {
      21             :   typedef std::pair<dart_unit_t, int> element_t;
      22             :   typedef dash::Array<element_t>      array_t;
      23             :   typedef array_t::const_iterator     const_it_t;
      24             :   typedef array_t::index_type         index_t;
      25           4 :   size_t local_size = 50;
      26           8 :   dash::Array<element_t> array(_dash_size * local_size);
      27             :   // Initialize local elements
      28           4 :   index_t l_off = 0;
      29         204 :   for (auto l_it = array.lbegin(); l_it != array.lend(); ++l_it, ++l_off) {
      30         200 :     *l_it = std::make_pair(_dash_id, l_off);
      31             :   }
      32             :   // Wait for all units to initialize their assigned range
      33           4 :   array.barrier();
      34             :   // Global ranges to copy are dealt to units from the back
      35             :   // to ensure most ranges are copied global-to-local.
      36           4 :   auto global_offset      = array.size() -
      37           4 :                             ((_dash_id + 1) * local_size);
      38           4 :   element_t * local_range = new element_t[local_size];
      39           4 :   const_it_t global_begin = array.begin() + global_offset;
      40             :   // Copy global element range to local memory
      41           4 :   std::copy(
      42             :     global_begin,
      43             :     global_begin + local_size,
      44           4 :     local_range);
      45             :   // Test and modify elements in local memory
      46         204 :   for (size_t li = 0; li < local_size; ++li) {
      47             :     // Check if local copies are identical to global values
      48         200 :     element_t g_element = array[global_offset + li];
      49         200 :     element_t l_element = local_range[li];
      50         200 :     ASSERT_EQ_U(
      51             :       g_element,
      52             :       l_element);
      53             :     // Modify local copies
      54         200 :     local_range[li].first   = _dash_id;
      55         200 :     local_range[li].second += 1000;
      56             :   }
      57             :   // Copy modified local elements back to global array
      58             :   std::copy(
      59             :       local_range,
      60             :       local_range + local_size,
      61           4 :       array.begin() + global_offset);
      62             :   // Test elements in global array
      63         204 :   for (size_t li = 0; li < local_size; ++li) {
      64         200 :     element_t g_element = array[global_offset + li];
      65         200 :     element_t l_element = local_range[li];
      66             :     // Plausibility checks of element
      67         200 :     ASSERT_EQ_U(g_element.first, _dash_id);
      68         200 :     ASSERT_EQ_U(g_element.second, 1000 + li);
      69         200 :     ASSERT_EQ_U(g_element, l_element);
      70             :   }
      71             :   // Free local memory
      72           4 :   delete[] local_range;
      73             : }
      74             : 
      75          20 : TEST_F(STLAlgorithmTest, StdCopyGlobalToGlobal) {
      76             :   typedef std::pair<dart_unit_t, int> element_t;
      77             :   typedef dash::Array<element_t>      array_t;
      78             :   typedef array_t::const_iterator     const_it_t;
      79             :   typedef array_t::index_type         index_t;
      80           4 :   size_t local_size = 5;
      81             :   // Source array:
      82           8 :   dash::Array<element_t> array_a(_dash_size * local_size);
      83             :   // Target array:
      84           8 :   dash::Array<element_t> array_b(_dash_size * local_size);
      85             :   // Initialize local elements:
      86           4 :   index_t lidx = 0;
      87          24 :   for (auto l_it = array_a.lbegin(); l_it != array_a.lend(); ++l_it, ++lidx) {
      88          20 :     *l_it = std::make_pair(_dash_id, lidx);
      89             :   }
      90             :   // Wait for all units to initialize their assigned range:
      91           4 :   array_a.barrier();
      92             : 
      93             :   // Global-to-global copy using std::copy:
      94           4 :   if (dash::myid() == 0) {
      95           1 :     std::copy(array_a.begin(), array_a.end(), array_b.begin());
      96             :   }
      97             :   // Wait until copy operation is completed:
      98           4 :   dash::barrier();
      99             : 
     100             :   // Validate values:
     101           4 :   lidx = 0;
     102          24 :   for (auto l_it = array_b.lbegin(); l_it != array_b.lend(); ++l_it, ++lidx) {
     103          20 :     ASSERT_EQ_U(array_a.local[lidx], static_cast<element_t>(*l_it));
     104             :   }
     105             : }
     106             : 
     107          20 : TEST_F(STLAlgorithmTest, StdAllOf) {
     108             :   typedef int                     element_t;
     109             :   typedef dash::Array<element_t>  array_t;
     110             :   typedef array_t::const_iterator const_it_t;
     111             :   typedef array_t::index_type     index_t;
     112           4 :   size_t local_size = 50;
     113             :   // Source array:
     114           8 :   dash::Array<element_t> array(_dash_size * local_size);
     115             :   // Initialize local elements:
     116           4 :   index_t lidx = 5;
     117         204 :   for (auto l_it = array.lbegin(); l_it != array.lend(); ++l_it, ++lidx) {
     118         200 :     *l_it = lidx;
     119             :   }
     120             :   // Wait for all units to initialize their assigned range:
     121           4 :   array.barrier();
     122             : 
     123           4 :   if (dash::myid() == 0) {
     124           2 :     bool all_gt_0 = std::all_of(array.begin(), array.end(),
     125         200 :                                 [](dash::GlobRef<element_t> r) {
     126         200 :                                   return r > 0;
     127         201 :                                 });
     128           2 :     bool all_gt_4 = std::all_of(array.begin(), array.end(),
     129         200 :                                 [](dash::GlobRef<element_t> r) {
     130         200 :                                   return r > 4;
     131         201 :                                 });
     132           2 :     bool all_gt_5 = std::all_of(array.begin(), array.end(),
     133           1 :                                 [](dash::GlobRef<element_t> r) {
     134           1 :                                   return r > 5;
     135           2 :                                 });
     136           1 :     ASSERT_TRUE(all_gt_0);
     137           1 :     ASSERT_TRUE(all_gt_4);
     138           1 :     ASSERT_FALSE(all_gt_5);
     139             :   }
     140          12 : }

Generated by: LCOV version 1.12