Added extra runner on Makefile
This commit is contained in:
11
Makefile
11
Makefile
@@ -16,7 +16,7 @@ $(OUT): $(SRC) $(LD_SCRIPT)
|
||||
clean:
|
||||
rm -f $(OUT) kernel.map
|
||||
|
||||
run: $(OUT)
|
||||
run-container: $(OUT)
|
||||
distrobox enter archlinux -- qemu-system-riscv32 \
|
||||
-machine virt \
|
||||
-bios default \
|
||||
@@ -24,3 +24,12 @@ run: $(OUT)
|
||||
-serial mon:stdio \
|
||||
-no-reboot \
|
||||
-kernel $(OUT)
|
||||
|
||||
run: $(OUT)
|
||||
qemu-system-riscv32 \
|
||||
-machine virt \
|
||||
-bios default \
|
||||
-nographic \
|
||||
-serial mon:stdio \
|
||||
-no-reboot \
|
||||
-kernel $(OUT)
|
||||
|
||||
83
example/alignment.c
Normal file
83
example/alignment.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
// Define the number of iterations for the benchmark
|
||||
#define ARRAY_SIZE 50000000 // 50 Million random data points
|
||||
|
||||
size_t align_up(size_t value, size_t align)
|
||||
{
|
||||
size_t remainder = value % align;
|
||||
if (remainder == 0)
|
||||
return value;
|
||||
return value + (align - remainder);
|
||||
}
|
||||
|
||||
size_t align_arithmetic(size_t value, size_t alignment)
|
||||
{
|
||||
return ((value + alignment - 1) / alignment) * alignment;
|
||||
}
|
||||
|
||||
size_t align_bitwise(size_t value, size_t alignment)
|
||||
{
|
||||
// TODO: assumendo che alignment è una potenza di 2
|
||||
return (value + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
void benchmark(const char *name,
|
||||
size_t (*func)(size_t, size_t),
|
||||
size_t alignment,
|
||||
const size_t *data_array,
|
||||
long long num_elements)
|
||||
{
|
||||
clock_t start, end;
|
||||
double cpu_time_used;
|
||||
size_t result_checksum = 0;
|
||||
|
||||
start = clock();
|
||||
for (long long i = 0; i < num_elements; i++) {
|
||||
result_checksum += func(data_array[i], alignment);
|
||||
}
|
||||
end = clock();
|
||||
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
|
||||
printf(" %-30s: %.4f seconds (Checksum: %zu)\n", name, cpu_time_used, result_checksum);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
size_t alignment_pot = 16;
|
||||
size_t alignment_non_pot = 7;
|
||||
|
||||
printf("Generating %lld random input values...\n", (long long)ARRAY_SIZE);
|
||||
srand((unsigned int)time(NULL));
|
||||
size_t *random_data = (size_t *)malloc(ARRAY_SIZE * sizeof(size_t));
|
||||
if (random_data == NULL) {
|
||||
fprintf(stderr, "Memory allocation failed!\n");
|
||||
return 1;
|
||||
}
|
||||
for (long long i = 0; i < ARRAY_SIZE; i++) {
|
||||
// pseudo-random values up to 1MB
|
||||
random_data[i] = rand() % (1024 * 1204);
|
||||
}
|
||||
printf("Data generation complete. \n\n");
|
||||
printf("--- Alignment Function Stress Benchmark (%lld Random Inputs) --\n\n", (long long)ARRAY_SIZE);
|
||||
|
||||
// ================================== SCENARIO 1: Power-of-Two Alignment ======================
|
||||
printf("Scenario 1: Alignment = %zu (Power of Two, Stress Test)\n", alignment_pot);
|
||||
|
||||
benchmark("1. Bitwise (PoT Specialized)", align_bitwise, alignment_pot, random_data, ARRAY_SIZE);
|
||||
benchmark("2. Arithmetic (General)", align_arithmetic, alignment_pot, random_data, ARRAY_SIZE);
|
||||
benchmark("3. Modulo (General)", align_up, alignment_pot, random_data, ARRAY_SIZE);
|
||||
|
||||
// ================================== SCENARIO 2: Non-Power-of-Two Alignment ======================
|
||||
printf("Scenario 2: Alignment = %zu (Non-Power of Two, Stress Test)\n", alignment_non_pot);
|
||||
|
||||
//benchmark("1. Bitwise (PoT Specialized)", align_bitwise, alignment_pot, random_data, ARRAY_SIZE);
|
||||
benchmark("2. Arithmetic (General)", align_arithmetic, alignment_non_pot, random_data, ARRAY_SIZE);
|
||||
benchmark("3. Modulo (General)", align_up, alignment_non_pot, random_data, ARRAY_SIZE);
|
||||
|
||||
free(random_data);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user