-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
60 lines (50 loc) · 1.55 KB
/
test.c
File metadata and controls
60 lines (50 loc) · 1.55 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
#include "ellipse.h"
#include <stdio.h>
#include <time.h>
// Cross-platform timing
#ifdef _WIN32
#include <windows.h>
#else
#include <time.h>
#include <sys/time.h>
#endif
// Include OpenBLAS threading control
#ifdef NAN_USE_OPENBLAS
#include <cblas.h>
#endif
// Portable wall-clock timing function
double get_time_in_seconds() {
#ifdef _WIN32
LARGE_INTEGER freq, t;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t);
return (double)t.QuadPart / freq.QuadPart;
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ts.tv_nsec / 1e9;
#endif
}
int main() {
Tensor* a = ones(FLOAT64, (int[]){1000,1000}, false);
Tensor* b = zeros(FLOAT64, (int[]){1000,1000}, false);
size_t n = a->size;
for(int i = 0; i < n; i++) {
b->data.float64[i] = (double)2.0;
}
// Optimize OpenBLAS threading (set to number of CPU cores)
#ifdef NAN_USE_OPENBLAS
// Use environment variable or runtime setting (e.g., 4 cores)
// Can be overridden with OPENBLAS_NUM_THREADS environment variable
int num_threads = 8; // Adjust based on your CPU
openblas_set_num_threads(num_threads);
printf("OpenBLAS using %d threads\n", openblas_get_num_threads());
#endif
// Time matrix multiplication
double start = get_time_in_seconds();
Tensor* c = matmul(a, b);
double end = get_time_in_seconds();
double time = end - start;
printf("a @ b: took %.6f sec\n", time);
return 0;
}