LCOV - code coverage report
Current view: top level - UTests - testParallelWrite.cpp (source / functions) Hit Total Coverage
Test: Coverage example Lines: 34 37 91.9 %
Date: 2021-12-02 17:21:05 Functions: 6 6 100.0 %

          Line data    Source code
       1             : ///////////////////////////////////////////////////////////////////////////
       2             : // Spetabaru - Berenger Bramas MPCDF - 2017
       3             : // Under LGPL Licence, please you must read the LICENCE file.
       4             : ///////////////////////////////////////////////////////////////////////////
       5             : 
       6             : #include <future>
       7             : 
       8             : #include "UTester.hpp"
       9             : 
      10             : #include "Data/SpDataAccessMode.hpp"
      11             : #include "Utils/SpUtils.hpp"
      12             : 
      13             : #include "Task/SpTask.hpp"
      14             : #include "Legacy/SpRuntime.hpp"
      15             : 
      16             : class TestParallelWrite : public UTester< TestParallelWrite > {
      17             :     using Parent = UTester< TestParallelWrite >;
      18             : 
      19           1 :     void TestBasic(){
      20             :         {
      21           2 :             SpRuntime runtime(2);
      22             : 
      23           1 :             std::atomic<int> initVal(0);
      24             : 
      25           3 :             for(int idxThread = 0 ; idxThread < runtime.getNbThreads() ; ++idxThread){
      26           0 :                 runtime.task(SpParallelWrite(initVal),
      27           2 :                              [&](std::atomic<int>& initValParam){
      28           2 :                     initValParam += 1;
      29           3 :                     while(initValParam != runtime.getNbThreads()){
      30           1 :                         usleep(100);
      31             :                     }
      32          14 :                 });
      33             :             }
      34             : 
      35           1 :             runtime.waitAllTasks();
      36             :         }
      37             :         {
      38           2 :             SpRuntime runtime(10);
      39             : 
      40           1 :             std::atomic<int> initVal(0);
      41             : 
      42          11 :             for(int idxThread = 0 ; idxThread < runtime.getNbThreads() ; ++idxThread){
      43           0 :                 runtime.task(SpParallelWrite(initVal),
      44          10 :                              [&](std::atomic<int>& initValParam){
      45          10 :                     initValParam += 1;
      46          33 :                     while(initValParam != runtime.getNbThreads()){
      47          23 :                         usleep(100);
      48             :                     }
      49          10 :                 });
      50             :             }
      51             : 
      52           1 :             runtime.waitAllTasks();
      53             :         }
      54             :         {
      55           2 :             SpRuntime runtime(10);
      56          22 :             std::promise<long int> promises[10];
      57             : 
      58           1 :             int dumbVal = 0;
      59             : 
      60          11 :             for(int idxThread = 0 ; idxThread < runtime.getNbThreads() ; ++idxThread){
      61           0 :                 runtime.task(SpParallelWrite(dumbVal),
      62          50 :                              [&,idxThread](int& /*dumbValParam*/){
      63          10 :                     promises[idxThread].set_value(idxThread);
      64          10 :                     const long int res = promises[(idxThread+1)%10].get_future().get();
      65          10 :                     UASSERTETRUE(res == (idxThread+1)%10);
      66          30 :                 });
      67             :             }
      68             : 
      69           1 :             runtime.waitAllTasks();
      70             :         }
      71           1 :     }
      72             : 
      73           1 :     void SetTests() {
      74           1 :         Parent::AddTest(&TestParallelWrite::TestBasic, "Basic test for parrallel write access");
      75           1 :     }
      76             : };
      77             : 
      78             : // You must do this
      79           1 : TestClass(TestParallelWrite)
      80             : 
      81             : 

Generated by: LCOV version 1.14