This commit is contained in:
2026-05-14 09:27:48 +08:00
parent 1100043d73
commit f8ae30583d
17 changed files with 1007 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <math.h>
#define MAX_THREADS 64
#define N 1000000000LL
long long global_sum = 0;
long long nelems_per_thread;
pthread_mutex_t mutex;
void *sum_squares_thread(void *arg) {
long long start = *(long long *)arg;
long long end = start + nelems_per_thread;
if (end > N) end = N;
long long local_sum = 0;
for (long long i = start; i < end; i++) {
local_sum += i * i;
}
pthread_mutex_lock(&mutex);
global_sum += local_sum;
pthread_mutex_unlock(&mutex);
return NULL;
}
double get_time() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <num_threads>\n", argv[0]);
return 1;
}
int nthreads = atoi(argv[1]);
if (nthreads > MAX_THREADS) nthreads = MAX_THREADS;
nelems_per_thread = N / nthreads;
pthread_mutex_init(&mutex, NULL);
pthread_t threads[MAX_THREADS];
long long starts[MAX_THREADS];
double t_start = get_time();
for (int i = 0; i < nthreads; i++) {
starts[i] = i * nelems_per_thread;
pthread_create(&threads[i], NULL, sum_squares_thread, &starts[i]);
}
for (int i = 0; i < nthreads; i++) {
pthread_join(threads[i], NULL);
}
double t_end = get_time();
double elapsed = t_end - t_start;
/* Expected sum of squares: (n-1)*n*(2n-1)/6 */
long long expected = (N - 1) * N * (2 * N - 1) / 6;
printf("Threads: %d, Sum of squares: %lld, Expected: %lld, Time: %.6f s\n",
nthreads, global_sum, expected, elapsed);
pthread_mutex_destroy(&mutex);
return 0;
}