numeric-linalg

Educational material on the SciPy implementation of numerical linear algebra algorithms

NameSizeMode
..
getrf/benchmarks/src/progress-bar.h 845B -rw-r--r--
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef PROGRESS_BAR_H_
#define PROGRESS_BAR_H_

#include <stdio.h>
#include <stdint.h>
#include <pthread.h>

#define PROGRESS_BAR_LENGTH 50

/*
 * A thread-safe self-printing progress bar
 */
typedef struct {
  size_t          total;
  size_t          count;
  pthread_mutex_t mutex;
} ProgressBar;

void progress_bar_inc(ProgressBar *p)
{
  pthread_mutex_lock(&p->mutex);

  p->count++;
  size_t filled_length = (PROGRESS_BAR_LENGTH*p->count)/p->total;
  size_t empty_length = PROGRESS_BAR_LENGTH - filled_length;

  printf("\r[");
  for (size_t i = 0; i < filled_length; i++) printf("=");
  for (size_t i = 0; i < empty_length; i++)  printf(" ");

  size_t percent = (100*p->count)/p->total;
  printf("] %3zu%%", percent);
  if (percent == 100) printf("\n");

  fflush(stdout);
  pthread_mutex_unlock(&p->mutex);
}

#endif // PROGRESS_BAR_H_