numeric-linalg

Educational material on the SciPy implementation of numerical linear algebra algorithms

File Name Size Mode
progress-bar.h 845B -rw-r--r--
 1 #ifndef PROGRESS_BAR_H_
 2 #define PROGRESS_BAR_H_
 3 
 4 #include <stdio.h>
 5 #include <stdint.h>
 6 #include <pthread.h>
 7 
 8 #define PROGRESS_BAR_LENGTH 50
 9 
10 /*
11  * A thread-safe self-printing progress bar
12  */
13 typedef struct {
14   size_t          total;
15   size_t          count;
16   pthread_mutex_t mutex;
17 } ProgressBar;
18 
19 void progress_bar_inc(ProgressBar *p)
20 {
21   pthread_mutex_lock(&p->mutex);
22 
23   p->count++;
24   size_t filled_length = (PROGRESS_BAR_LENGTH*p->count)/p->total;
25   size_t empty_length = PROGRESS_BAR_LENGTH - filled_length;
26 
27   printf("\r[");
28   for (size_t i = 0; i < filled_length; i++) printf("=");
29   for (size_t i = 0; i < empty_length; i++)  printf(" ");
30 
31   size_t percent = (100*p->count)/p->total;
32   printf("] %3zu%%", percent);
33   if (percent == 100) printf("\n");
34 
35   fflush(stdout);
36   pthread_mutex_unlock(&p->mutex);
37 }
38 
39 #endif // PROGRESS_BAR_H_