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 TestCommutativeWrite : public UTester< TestCommutativeWrite > { 17 : using Parent = UTester< TestCommutativeWrite >; 18 : 19 1 : void TestBasic(){ 20 : { 21 2 : SpRuntime runtime(10); 22 2 : std::promise<int> delay1; 23 : 24 1 : int dumbVal = 0; 25 1 : int commuteVal = 0; 26 : 27 0 : auto descr0 = runtime.task(SpWrite(dumbVal), 28 1 : [&](int& dumbValParam){ 29 1 : delay1.get_future().get(); 30 1 : dumbValParam = 1; 31 3 : }); 32 1 : UASSERTETRUE(descr0.isReady() == true); 33 : 34 1 : auto descr1 = runtime.task(SpRead(dumbVal), SpCommutativeWrite(commuteVal), 35 1 : [&](const int& dumbValParam, int& commuteValParam){ 36 1 : UASSERTETRUE(dumbValParam == 1); 37 1 : UASSERTETRUE(commuteValParam == 1); 38 6 : }); 39 1 : UASSERTETRUE(descr1.isReady() == false); 40 : 41 0 : auto descr2 = runtime.task(SpCommutativeWrite(commuteVal), 42 1 : [&](int& commuteValParam){ 43 1 : UASSERTETRUE(commuteValParam == 0); 44 1 : commuteValParam = 1; 45 2 : }); 46 1 : UASSERTETRUE(descr2.isReady() == true); 47 : 48 1 : delay1.set_value(0); 49 : 50 1 : runtime.waitAllTasks(); 51 : } 52 1 : std::cout << "Next..." << std::endl; 53 : { 54 2 : SpRuntime runtime(2); 55 : 56 1 : std::atomic<int> initVal(0); 57 : 58 3 : for(int idxThread = 0 ; idxThread < runtime.getNbThreads() ; ++idxThread){ 59 0 : runtime.task(SpCommutativeWrite(initVal), 60 2 : [&](std::atomic<int>& initValParam){ 61 2 : UASSERTETRUE(initValParam == 0); 62 2 : initValParam += 1; 63 2 : usleep(1000); 64 2 : UASSERTETRUE(initValParam == 1); 65 2 : initValParam -= 1; 66 16 : }); 67 : } 68 : 69 1 : runtime.waitAllTasks(); 70 : } 71 1 : std::cout << "Next..." << std::endl; 72 : { 73 2 : SpRuntime runtime(10); 74 : 75 1 : std::atomic<int> initVal(0); 76 : 77 11 : for(int idxThread = 0 ; idxThread < runtime.getNbThreads() ; ++idxThread){ 78 0 : runtime.task(SpCommutativeWrite(initVal), 79 10 : [&](std::atomic<int>& initValParam){ 80 10 : UASSERTETRUE(initValParam == 0); 81 10 : initValParam += 1; 82 10 : usleep(1000); 83 10 : UASSERTETRUE(initValParam == 1); 84 10 : initValParam -= 1; 85 20 : }); 86 : } 87 : 88 1 : runtime.waitAllTasks(); 89 : } 90 1 : } 91 : 92 1 : void SetTests() { 93 1 : Parent::AddTest(&TestCommutativeWrite::TestBasic, "Basic test for commutative write access"); 94 1 : } 95 : }; 96 : 97 : // You must do this 98 1 : TestClass(TestCommutativeWrite) 99 : 100 :