Namespaces
Variants
Views
Actions

Talk:cpp/chrono/duration

From cppreference.com

std::chrono::duration could not company with openmpi MPI_gather

following code will be wrong when we use more than 10 threads

  1. #include <iostream>
  2. #include "mpi.h"
  3. #include "stdlib.h"
  4. #include <thread>
  5. #include <chrono>
  6. #include "stdlib.h"
  7. using namespace std;
  8.  
  9.  
  10. void delay( int max_time){
  11.     srand(time(0));
  12.     int sleepnum = rand()%max_time;
  13.     std::chrono::milliseconds dura(sleepnum);
  14.     this_thread::sleep_for(dura);
  15. }
  16.  
  17. int main(){
  18.     MPI_Init(NULL,NULL);
  19.     int size, rank;
  20.     MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  21.     MPI_Comm_size(MPI_COMM_WORLD,&size);
  22.     double local_num;
  23.     double* buffer = new double[(size)*sizeof(double)  ];
  24.     cout<<"pin1\n";
  25.     auto start = std::chrono::high_resolution_clock::now();
  26.     cout<<"pin2\n";	
  27. //    for(int i = 0 ; i < 1000; i++)
  28.     {
  29.         cout<<"pin3\n";
  30.         //delay(1000);
  31.  
  32.         MPI_Gather((void*)&local_num,1,MPI_DOUBLE,(void*)buffer,size,MPI_DOUBLE,0,MPI_COMM_WORLD);
  33.         if(rank == 0){
  34.             //delay(100);
  35.         }
  36.         cout<<"pin4\n";
  37.         //MPI_Bcast(&local_num, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  38.         cout<<"pin5\n";
  39.     }
  40.     auto end = std::chrono::high_resolution_clock::now();
  41.     std::chrono::duration<double, std::milli> elapsed = end-start;
  42.     std::cout << "use " << elapsed.count() << " ms\n";
  43.     //delete[] buffer;
  44.     MPI_Finalize();
  45. }

I fixed the formatting for the above, but I don't think this belongs here - this isn't stackoverflow. MPI coding is complicated and I wouldn't be surprised if there were a bug or two in the above code.

I think it should be removed.

Hadriel (talk) 14:10, 12 December 2019 (PST)

[edit] ADL and Duration Literals

Is there a way to get argument dependent lookup to work with duration literals? If so, that would be a nice example. This would avoid the need to add a using statement just to use a literal somewhere.

e.g.

 void foo(std::chrono::duration d) {
 }
 
 ...
 
 foo(10ms); // 10ms is understood to be a std::chrono::duration, so no using is required

68.249.219.17 15:52, 29 December 2021 (PST)

ADL would let you find foo if it were defined in namespace std or std::chrono or in the class std::chrono::duration when given an std::chrono::duration argument. The opposite direction of having a function that takes an argument from std::chrono::duration, that has no bearing on how the argument is interpreted when calling foo(argument). Tl;dr ADL changes the function called, not the argument. --Ybab321 (talk) 04:22, 30 December 2021 (PST)