/************************* Copyright By PowCoder代写 加微信 powcoder #include double *A; // input vector // mutex and condition variable for barrier type of synchronization void *thread_scan(void *id); int main(int argc, char *argv[]) if(argc == 3){ //initialize mutex, condition variable and allocate space for thread ids // Initialise the vectors //print results //print the running time pthread_exit(NULL); void *thread_scan(void *id){ my_id = (int*) id; //partition vector as evenly as possible – diff <= 1
int n_i = N / num_thrds;
int n_r = N % num_thrds;
i_start = *my_id * n_i;
if (*my_id < n_r){
i_start += *my_id;
i_start += n_r;
i_end = i_start + n_i;
//step 1 computation, all threads
C[i_start] = A[i_start];
for (int i=i_start+1; i < i_end; i++)
C[i] = C[i-1] + A[i];
W[*my_id] = C[i_end-1]; // Store the last element in work vector
// Wait untill all threads completed step 1
pthread_mutex_lock(&barrier_mutex);
barrier_count++;
if (barrier_count == num_thrds) {
// All threads completed step 1
barrier_count = 0;
//The last completed thread performs seq computation for step 2
for (int i=1; i < num_thrds-1; i++)
W[i] = W[i] + W[i-1];
// The last thread awakens all threads
pthread_cond_broadcast(&ok_to_proceed);
// Unlock mutex and put thread to sleep.
pthread_cond_wait(&ok_to_proceed, &barrier_mutex);
pthread_mutex_unlock(&barrier_mutex);
// step 3 computation, all threads except thread 0
if (*my_id > 0){
* A pthread program for parallel scan presented in lecture 4-2
*************************/
#include
#include
#include
#include
double *C, *C0; // output vectors and C0 used for sequential computation
double *W; // work vector for small sequential scan in the second step
int N; // vector size
int num_thrds; // thread number
pthread_mutex_t barrier_mutex;
pthread_cond_t ok_to_proceed;
int barrier_count = 0;
void print_vector(double* T, int n);
pthread_t *threads; // System thread ids
int *t_ids; // user defined thread ids
struct timeval start_time, end_time;
N = atoi(argv[1]);
num_thrds = atoi(argv[2]);
printf(“N = %d, num_thrds = %d, \n\n”, N, num_thrds);
printf(“Usage: %s N num_thrds\n\n”
” N: vector length\n”
” num_thrds: number of threads\n\n”,argv[0]);
pthread_mutex_init(&barrier_mutex, NULL);
pthread_cond_init(&ok_to_proceed, NULL);
threads = malloc(num_thrds*sizeof(pthread_t));
t_ids = malloc(num_thrds*sizeof(int));
A = (double*)malloc(N*sizeof(double));
C = (double*)malloc(N*sizeof(double));
C0 = (double*)malloc(N*sizeof(double));
for (int i=0; i < N; i++){
// A[i] = (double) i;
A[i] = (double) rand() / RAND_MAX; //elements < 1.0
C[i] = 0.0;
C0[i] = 0.0;
//print results
// print_vector(A, N);
W = (double*)malloc(num_thrds*sizeof(double)); // work vector used in second step
for (int i=0; i < num_thrds; i++)
W[i] = 0.0;
gettimeofday(&start_time, 0);
for(int i=0; i < num_thrds; i++) {
t_ids[i] = i;
// printf("Creating thread %d\n", i);
rc = pthread_create(&threads[i], NULL, thread_scan, (void *) &t_ids[i]);
printf("ERROR; return code from pthread_create() is %d\n", rc);
/* join the threads here */
for (int i=0; i < num_thrds; i++) {
rc = pthread_join(threads[i], NULL);
printf("ERROR; return code from pthread_join() is %d\n", rc);
gettimeofday(&end_time, 0);
// Sequential computation
C0[0] = A[0];
for (int i = 1; i < N; i++)
C0[i] = C0[i-1] + A[i];
// Compare the results
int cnt = 0;
for (int i = 0; i < N; i++)
if ((C[i]-C0[i]) > 1.0E-6) cnt += 1;
if (cnt !=0)
printf(“Results are not equal – something wrong with parallel scan.\n\n”);
printf(“Results are equal.\n\n”);
// print_vector(C, N);
// print_vector(C0, N);
long seconds = end_time.tv_sec – start_time.tv_sec;
long microseconds = end_time.tv_usec – start_time.tv_usec;
double elapsed = seconds + 1e-6 * microseconds;
printf(“it took %f seconds to complete.\n\n”, elapsed);
int *my_id;
int i_start, i_end;
double w_i = W[*my_id-1];
for (int i = i_start; i < i_end; i++)
C[i] = C[i] + w_i;
pthread_exit(NULL);
void print_vector(double* T, int n){
for (int i=0; i < n; i++)
printf("%.2f ", T[i]);
printf("\n\n");
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com