diff --git a/F74086713/lab_12_time/G_1.md b/F74086713/lab_12_time/G_1.md new file mode 100644 index 0000000..dcb18bf --- /dev/null +++ b/F74086713/lab_12_time/G_1.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 1 +#define D_RDTSC 0 +#define D_GETTIME 1 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.138316 | 0.000000 | 0.042528 | 0.045 | +| trial 1 | 3.138316 | 0.000000 | 0.041640 | 0.044 | +| trial 2 | 3.138316 | 0.000000 | 0.041792 | 0.044 | +| trial 3 | 3.138316 | 0.000000 | 0.042085 | 0.045 | +| trial 4 | 3.138316 | 0.000000 | 0.041971 | 0.045 | +| trial 5 | 3.138316 | 0.000000 | 0.041302 | 0.044 | +| trial 6 | 3.138316 | 0.000000 | 0.042573 | 0.054 | +| trial 7 | 3.138316 | 0.000000 | 0.042770 | 0.045 | +| trial 8 | 3.138316 | 0.000000 | 0.041754 | 0.045 | +| trial 9 | 3.138316 | 0.000000 | 0.042302 | 0.045 | diff --git a/F74086713/lab_12_time/G_10.md b/F74086713/lab_12_time/G_10.md new file mode 100644 index 0000000..6399960 --- /dev/null +++ b/F74086713/lab_12_time/G_10.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 10 +#define D_RDTSC 0 +#define D_GETTIME 1 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.138354 | 0.000000 | 0.041841 | 0.421 | +| trial 1 | 3.139321 | 0.000000 | 0.043010 | 0.433 | +| trial 2 | 3.139321 | 0.000000 | 0.043449 | 0.437 | +| trial 3 | 3.139321 | 0.000000 | 0.041818 | 0.421 | +| trial 4 | 3.139631 | 0.000000 | 0.042357 | 0.427 | +| trial 5 | 3.139631 | 0.000000 | 0.042768 | 0.430 | +| trial 6 | 3.139374 | 0.000000 | 0.041555 | 0.419 | +| trial 7 | 3.139374 | 0.000000 | 0.042045 | 0.424 | +| trial 8 | 3.139143 | 0.000000 | 0.042304 | 0.426 | +| trial 9 | 3.139143 | 0.000000 | 0.042771 | 0.432 | diff --git a/F74086713/lab_12_time/G_100.md b/F74086713/lab_12_time/G_100.md new file mode 100644 index 0000000..b86d485 --- /dev/null +++ b/F74086713/lab_12_time/G_100.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 100 +#define D_RDTSC 0 +#define D_GETTIME 1 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.139117 | 0.000000 | 0.042178 | 4.221 | +| trial 1 | 3.139488 | 0.000000 | 0.043114 | 4.314 | +| trial 2 | 3.139476 | 0.000000 | 0.043685 | 4.371 | +| trial 3 | 3.139584 | 0.000000 | 0.042400 | 4.243 | +| trial 4 | 3.139554 | 0.000000 | 0.041878 | 4.190 | +| trial 5 | 3.139197 | 0.000000 | 0.042060 | 4.209 | +| trial 6 | 3.139181 | 0.000000 | 0.042515 | 4.254 | +| trial 7 | 3.139216 | 0.000000 | 0.041951 | 4.199 | +| trial 8 | 3.139116 | 0.000000 | 0.043369 | 4.341 | +| trial 9 | 3.139523 | 0.000000 | 0.042715 | 4.274 | diff --git a/F74086713/lab_12_time/R_1.md b/F74086713/lab_12_time/R_1.md new file mode 100644 index 0000000..407c744 --- /dev/null +++ b/F74086713/lab_12_time/R_1.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 1 +#define D_RDTSC 1 +#define D_GETTIME 0 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.143032 | 0.042294 | 0.000000 | 0.045 | +| trial 1 | 3.143032 | 0.041880 | 0.000000 | 0.045 | +| trial 2 | 3.143032 | 0.042306 | 0.000000 | 0.045 | +| trial 3 | 3.143032 | 0.042010 | 0.000000 | 0.045 | +| trial 4 | 3.143032 | 0.043833 | 0.000000 | 0.046 | +| trial 5 | 3.143032 | 0.041769 | 0.000000 | 0.045 | +| trial 6 | 3.143032 | 0.041953 | 0.000000 | 0.046 | +| trial 7 | 3.143032 | 0.042780 | 0.000000 | 0.046 | +| trial 8 | 3.136320 | 0.041813 | 0.000000 | 0.044 | +| trial 9 | 3.136320 | 0.045151 | 0.000000 | 0.048 | diff --git a/F74086713/lab_12_time/R_10.md b/F74086713/lab_12_time/R_10.md new file mode 100644 index 0000000..232342c --- /dev/null +++ b/F74086713/lab_12_time/R_10.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 10 +#define D_RDTSC 1 +#define D_GETTIME 0 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.138462 | 0.041565 | 0.000000 | 0.418 | +| trial 1 | 3.138462 | 0.043313 | 0.000000 | 0.438 | +| trial 2 | 3.140276 | 0.044828 | 0.000000 | 0.451 | +| trial 3 | 3.140276 | 0.042138 | 0.000000 | 0.424 | +| trial 4 | 3.138439 | 0.042947 | 0.000000 | 0.432 | +| trial 5 | 3.138439 | 0.041612 | 0.000000 | 0.420 | +| trial 6 | 3.140058 | 0.041777 | 0.000000 | 0.420 | +| trial 7 | 3.140058 | 0.041936 | 0.000000 | 0.422 | +| trial 8 | 3.140058 | 0.041743 | 0.000000 | 0.420 | +| trial 9 | 3.140721 | 0.042269 | 0.000000 | 0.426 | diff --git a/F74086713/lab_12_time/R_100.md b/F74086713/lab_12_time/R_100.md new file mode 100644 index 0000000..45a5193 --- /dev/null +++ b/F74086713/lab_12_time/R_100.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 100 +#define D_RDTSC 1 +#define D_GETTIME 0 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.139732 | 0.042229 | 0.000000 | 4.226 | +| trial 1 | 3.139171 | 0.041970 | 0.000000 | 4.200 | +| trial 2 | 3.139514 | 0.043300 | 0.000000 | 4.333 | +| trial 3 | 3.139461 | 0.042658 | 0.000000 | 4.269 | +| trial 4 | 3.139330 | 0.042689 | 0.000000 | 4.272 | +| trial 5 | 3.139469 | 0.043230 | 0.000000 | 4.326 | +| trial 6 | 3.139297 | 0.042185 | 0.000000 | 4.221 | +| trial 7 | 3.139116 | 0.042656 | 0.000000 | 4.268 | +| trial 8 | 3.139279 | 0.043051 | 0.000000 | 4.308 | +| trial 9 | 3.139201 | 0.042019 | 0.000000 | 4.205 | diff --git a/F74086713/lab_12_time/T_1.md b/F74086713/lab_12_time/T_1.md new file mode 100644 index 0000000..9fcd8c0 --- /dev/null +++ b/F74086713/lab_12_time/T_1.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 1 +#define D_RDTSC 0 +#define D_GETTIME 0 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.140140 | 0.000000 | 0.000000 | 0.045 | +| trial 1 | 3.140140 | 0.000000 | 0.000000 | 0.045 | +| trial 2 | 3.140140 | 0.000000 | 0.000000 | 0.046 | +| trial 3 | 3.140140 | 0.000000 | 0.000000 | 0.045 | +| trial 4 | 3.140140 | 0.000000 | 0.000000 | 0.046 | +| trial 5 | 3.140140 | 0.000000 | 0.000000 | 0.045 | +| trial 6 | 3.140140 | 0.000000 | 0.000000 | 0.045 | +| trial 7 | 3.140140 | 0.000000 | 0.000000 | 0.045 | +| trial 8 | 3.140140 | 0.000000 | 0.000000 | 0.044 | +| trial 9 | 3.140140 | 0.000000 | 0.000000 | 0.045 | diff --git a/F74086713/lab_12_time/T_10.md b/F74086713/lab_12_time/T_10.md new file mode 100644 index 0000000..3bf2cf3 --- /dev/null +++ b/F74086713/lab_12_time/T_10.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 10 +#define D_RDTSC 0 +#define D_GETTIME 0 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.139791 | 0.000000 | 0.000000 | 0.419 | +| trial 1 | 3.139791 | 0.000000 | 0.000000 | 0.424 | +| trial 2 | 3.139791 | 0.000000 | 0.000000 | 0.429 | +| trial 3 | 3.139097 | 0.000000 | 0.000000 | 0.450 | +| trial 4 | 3.139097 | 0.000000 | 0.000000 | 0.425 | +| trial 5 | 3.137966 | 0.000000 | 0.000000 | 0.423 | +| trial 6 | 3.137966 | 0.000000 | 0.000000 | 0.429 | +| trial 7 | 3.139175 | 0.000000 | 0.000000 | 0.424 | +| trial 8 | 3.139175 | 0.000000 | 0.000000 | 0.422 | +| trial 9 | 3.139175 | 0.000000 | 0.000000 | 0.421 | diff --git a/F74086713/lab_12_time/T_100.md b/F74086713/lab_12_time/T_100.md new file mode 100644 index 0000000..6191ba8 --- /dev/null +++ b/F74086713/lab_12_time/T_100.md @@ -0,0 +1,20 @@ +clock rate: +CPU MHz: 2208.000 +// **Configuration** +#define ROUNDS 100 +#define D_RDTSC 0 +#define D_GETTIME 0 +// **Configuration** + +| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) | +|-:|-:|-:|-:|-:| +| trial 0 | 3.139144 | 0.000000 | 0.000000 | 4.348 | +| trial 1 | 3.139513 | 0.000000 | 0.000000 | 4.255 | +| trial 2 | 3.139295 | 0.000000 | 0.000000 | 4.233 | +| trial 3 | 3.139682 | 0.000000 | 0.000000 | 4.237 | +| trial 4 | 3.139141 | 0.000000 | 0.000000 | 4.219 | +| trial 5 | 3.139394 | 0.000000 | 0.000000 | 4.233 | +| trial 6 | 3.139376 | 0.000000 | 0.000000 | 4.259 | +| trial 7 | 3.139133 | 0.000000 | 0.000000 | 4.251 | +| trial 8 | 3.139272 | 0.000000 | 0.000000 | 4.247 | +| trial 9 | 3.139551 | 0.000000 | 0.000000 | 4.185 | diff --git a/F74086713/lab_12_time/pi.c b/F74086713/lab_12_time/pi.c new file mode 100644 index 0000000..d87b00e --- /dev/null +++ b/F74086713/lab_12_time/pi.c @@ -0,0 +1,184 @@ +// **Configuration** +#define ROUNDS 100 +#define D_RDTSC 0 +#define D_GETTIME 1 +// **Configuration** + +#include +#include +#include +#include + +double get_cpu_rate() { + + // Ref: https://stackoverflow.com/a/646254/6734174 + + FILE *fp; + + char buffer[20]; + + double ret = 0; + + const char* cmd = "lscpu | grep -oP \'MHz:\\s+\\K.*\' | head -n 1"; + + fp = popen(cmd, "r"); + + if (fp == NULL) { + + printf("Failed to run command\n" ); + + exit(1); + + } + + while (fgets(buffer, sizeof(buffer), fp) != NULL) { + + ret = atof(buffer); + + } + + pclose(fp); + + return ret; + +} + + + +#if D_RDTSC + +/* assembly code to read the TSC */ + +static inline uint64_t RDTSC() { + + unsigned int hi, lo; + + __asm__ volatile("rdtsc" : "=a" (lo), "=d" (hi)); + + return ((uint64_t)hi << 32) | lo; + +} + +#endif + + + +#if D_GETTIME + +double GETTIME() { + + struct timespec ts; + + double sec; + + clock_gettime(CLOCK_REALTIME, &ts); + + sec = ts.tv_nsec; + + sec /= 1e9; + + sec += ts.tv_sec; + + return sec; + +} + +#endif + + + +int main(int argc, char** argv) + +{ + + const uint64_t interval = 1000; + + double rand_x, rand_y, origin_dist; + + uint64_t circle_points = 0, square_points = 0; + + uint64_t measure_rdtsc = 0; + + double measure_gettime = 0; + + double arr_pi[ROUNDS] = {}; + + srand(time(NULL)); + + + +#if D_RDTSC + + uint64_t c1 = RDTSC(); + +#endif + +#if D_GETTIME + + double t1 = GETTIME(); + +#endif + + + + for (int r = 0; r < ROUNDS; r++) { + + for (uint64_t i = 0; i < (interval * interval); i++) { + + rand_x = (double)(rand() % (interval + 1)) / interval; + + rand_y = (double)(rand() % (interval + 1)) / interval; + + origin_dist = rand_x * rand_x + rand_y * rand_y; + + if (origin_dist <= 1.0) + + circle_points++; + + square_points++; + + } + + arr_pi[r] = (double)(4 * circle_points) / square_points; + + } + + + +#if D_RDTSC + + uint64_t cycle = RDTSC()-c1; + + measure_rdtsc = cycle; + +#endif + +#if D_GETTIME + + double time_s = GETTIME()-t1; + + measure_gettime = time_s; + +#endif + + + + double sum_pi = 0; + + for(int i = 0; i < ROUNDS; i++) { + + sum_pi += arr_pi[i]; + + } + + double cpu_rate = get_cpu_rate(); + + printf(" %.6lf |", (double)sum_pi/ROUNDS); + + printf(" %.6lf |", (double)measure_rdtsc/ROUNDS/(cpu_rate*1000000)); + + printf(" %.6lf | ", (double)measure_gettime/ROUNDS); + + return 0; + +} diff --git a/F74086713/lab_12_time/run_test.sh b/F74086713/lab_12_time/run_test.sh new file mode 100644 index 0000000..8972c07 --- /dev/null +++ b/F74086713/lab_12_time/run_test.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +TIMEFORMAT=%R + +gcc -O2 -o pi pi.c + + + +function repeat() { + + for ((i=0;i<$1;i++)); do + + printf "| trial $i | %s |\n" "$(eval time ${*:2} 2>&1 | tr -d '\n')" + + done + +} + + + +printf "clock rate:\n" + +lscpu | grep MHz | head -n 1 + + + +# Print the configuration + +head -n 6 pi.c + +printf "| Trials | pi | Cycle/CPU rate (s) | Get time (s) | time command (s) |\n" + +printf "|-:|-:|-:|-:|-:|\n" + +repeat 10 ${*:1} + +rm pi + + + +unset TIMEFORMAT