planc
Parallel Lowrank Approximation with Non-negativity Constraints
distutils.hpp
Go to the documentation of this file.
1 /* Copyright 2016 Ramakrishnan Kannan */
2 #ifndef COMMON_DISTUTILS_HPP_
3 #define COMMON_DISTUTILS_HPP_
4 
5 #include <mpi.h>
6 #include <string>
7 #include "common/distutils.h"
8 #include "common/utils.h"
9 #include "common/utils.hpp"
10 
11 inline void mpitic() {
12  // tictoc_stack.push(clock());
13  tictoc_stack.push(std::chrono::steady_clock::now());
14 }
15 
16 inline void mpitic(int rank) {
17  // std::cout << "tic::" << rank << "::" << std::chrono::steady_clock::now() <<
18  // std::endl;
19  tictoc_stack.push(std::chrono::steady_clock::now());
20 }
21 
22 inline double mpitoc(int rank) {
23 #ifdef __WITH__BARRIER__TIMING__
24  MPI_Barrier(MPI_COMM_WORLD);
25 #endif
26  std::chrono::duration<double> time_span =
27  std::chrono::duration_cast<std::chrono::duration<double>>(
28  std::chrono::steady_clock::now() - tictoc_stack.top());
29  double rc = time_span.count();
30  tictoc_stack.pop();
31  std::cout << "toc::" << rank << "::" << rc << std::endl;
32  return rc;
33 }
34 
35 inline double mpitoc() {
36 #ifdef __WITH__BARRIER__TIMING__
37  MPI_Barrier(MPI_COMM_WORLD);
38 #endif
39  std::chrono::duration<double> time_span =
40  std::chrono::duration_cast<std::chrono::duration<double>>(
41  std::chrono::steady_clock::now() - tictoc_stack.top());
42  double rc = time_span.count();
43  tictoc_stack.pop();
44  return rc;
45 }
49 inline void memusage(const int myrank, std::string event) {
50  // Based on the answer from stackoverflow
51  // http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
52  int64_t rss = 0L;
53  FILE *fp = NULL;
54  if ((fp = fopen("/proc/self/statm", "r")) == NULL) return;
55  if (fscanf(fp, "%*s%ld", &rss) != 1) {
56  fclose(fp);
57  return;
58  }
59  fclose(fp);
60  int64_t current_proc_mem = (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
61  // INFO << myrank << "::mem::" << current_proc_mem << std::endl;
62  int64_t allprocmem;
63  MPI_Reduce(&current_proc_mem, &allprocmem, 1, MPI_INT64_T, MPI_SUM, 0,
64  MPI_COMM_WORLD);
65  if (myrank == 0) {
66  INFO << event << " total rss::" << allprocmem << std::endl;
67  }
68 }
69 
78 inline int itersplit(int n, int p, int r) {
79  int split = (r < n % p) ? n / p + 1 : n / p;
80  return split;
81 }
82 
90 inline int startidx(int n, int p, int r) {
91  int rem = n % p;
92  int idx =
93  (r < rem) ? r * (n / p + 1) : (rem * (n / p + 1) + ((r - rem) * (n / p)));
94  return idx;
95 }
96 #endif // COMMON_DISTUTILS_HPP_
double mpitoc(int rank)
Definition: distutils.hpp:22
int startidx(int n, int p, int r)
Returns the start idx of the current rank r for a global dimension n across p processes.
Definition: distutils.hpp:90
int itersplit(int n, int p, int r)
The dimension a particular rank holds out of the global dimension n across p processes.
Definition: distutils.hpp:78
void mpitic()
Definition: distutils.hpp:11
static std::stack< std::chrono::steady_clock::time_point > tictoc_stack
Definition: utils.hpp:37
#define INFO
Definition: utils.h:36
void memusage(const int myrank, std::string event)
Captures the memory usage of every mpi process.
Definition: distutils.hpp:49