-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_utils.c
More file actions
129 lines (110 loc) · 3 KB
/
file_utils.c
File metadata and controls
129 lines (110 loc) · 3 KB
1
2
3
4
5
6
7
8
9
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#define ONE_OVER_BILLION 1E-9
// Implementation of functions defined in file_utils.h
#include <stdio.h>
#include <string.h>
#include <structures.h>
#include <stdlib.h>
// all intrinsics?
#include <immintrin.h>
#include <omp.h>
void
print256_num(__m256d var) {
DTYPE *val = (DTYPE*) &var;
printf("Numerical: %f %f %f %f \n",
val[0], val[1], val[2], val[3]);
}
void
print_to_console(DTYPE * ptr, size_t size) {
for (size_t el=0; el<size; ++el) {
// DTYPE DEPENDENT
// for now: selected double!
printf("el[%ld]=%lf, ", el, (double) *(ptr+el));
// DTYPE DEPENDENT
}
printf("\n");
}
void
print_to_console_local(const struct lin_storage * storage) {
#pragma omp parallel
{
int tid = omp_get_thread_num();
int nthreads = omp_get_num_threads();
size_t partition_size = storage->size / nthreads;
printf("partition_size = %zu / %d = %zu\n", storage->size, nthreads, storage->size / nthreads);
#pragma omp for ordered schedule(static,1)
for (int t=0; t<nthreads; ++t)
{
#pragma omp ordered
{
printf("Thread %d printing output partition of size %zu\n", tid, partition_size);
print_to_console(storage->local_data[tid], partition_size);
}
}
}
}
void
print_to_console_double(double * ptr, size_t size) {
for (size_t el=0; el<size; ++el) {
//printf("el[%ld]=%lf, ", el, *(ptr+el));
printf("el[%ld]=%23.16e, ", el, *(ptr+el));
}
printf("\n");
}
void
print_to_console_int(int * ptr, size_t size) {
for (size_t el=0; el<size; ++el) {
printf("el[%ld]=%d, ", el, *(ptr+el));
}
printf("\n");
}
void
print_to_console_sizet(const size_t * const ptr, const size_t size) {
for (size_t el=0; el<size; ++el) {
printf("el[%ld]=%zu, ", el, *(ptr+el));
}
printf("\n");
}
void
print_exec_time(const char* fun, const struct timespec start, const struct timespec stop) {
double exec_time = (stop.tv_sec - start.tv_sec) +
(double)(stop.tv_nsec - start.tv_nsec) * (double)ONE_OVER_BILLION;
printf("%s: %fs\n", fun, exec_time);
}
void
save_to_file(const char const * filename, char* mode, DTYPE * ptr, size_t size) {
FILE *f = fopen(filename, mode);
if (f) {
// Save the array-like object consisting of int
for (size_t el=0; el<size; ++el) {
// DTYPE DEPENDENT
fprintf(f, "%.2f", (double) *(ptr+el));
fprintf(f, ",");
}
} else {
printf("Error: File could not be opened.");
}
if (ferror(f)) {
printf("Error while writing to a file.\n");
}
if (f != NULL) {
fclose(f);
}
}
void
fprint_array_sizet(FILE * const file_handle, const struct array p_array, const size_t size) {
for(int i=0; i<(int) size; ++i) {
(void) fprintf(file_handle, "%zu;", p_array.a[i]);
}
}
void
fprint_ptr_sizet(FILE * const file_handle, const size_t * const p_array, const size_t size) {
for(int i=0; i<(int) size; ++i) {
(void) fprintf(file_handle, "%zu;", p_array[i]);
}
}
void
fprint_array_int(FILE * const file_handle, const int * array, const size_t size) {
for(int i=0; i<(int) size; ++i) {
(void) fprintf(file_handle, "%d;", array[i]);
}
}