CS61C (su20) lab10

cs61c_lab10

Part1: Multi-threading programming using OpenMP

Exercise 2 - Vector Addition

方法1如下:

void v_add_optimized_adjacent(double* x, double* y, double* z) {
     #pragma omp parallel
	{
        int num_threads = omp_get_num_threads();
        int id = omp_get_thread_num();
		for(int i=id; i<ARRAY_SIZE; i+= num_threads)
			z[i] = x[i] + y[i];
	}
}

方法二如下:

void v_add_optimized_chunks(double* x, double* y, double* z) {
          #pragma omp parallel
	{
        int num_threads = omp_get_num_threads();
        int id = omp_get_thread_num();
        if (id != num_threads - 1) {
            for(int i=ARRAY_SIZE/num_threads*id; i<ARRAY_SIZE/num_threads*(id + 1); i++)
			    z[i] = x[i] + y[i];
        }
        else {
            for(int i=ARRAY_SIZE/num_threads*id; i<ARRAY_SIZE; i++)
			    z[i] = x[i] + y[i];
        }
	}
}
Exercise 3 - Dot Product

不使用reduction的代码如下,不知道题目想干什么,这部分参考了1 ,感觉还是不是很符合题意:

double dotp_manual_optimized(double* x, double* y, int arr_size) {
  double global_sum = 0.0;
  int num_threads; 
  #pragma omp parallel
  {
    num_threads = omp_get_num_threads();
  }
  double *sum = (double *)malloc(num_threads * sizeof(double));
  memset(sum, 0, sizeof(sum));
#pragma omp parallel
  {
    int id = omp_get_thread_num();
    double now = 0.0;
    for (int i = id; i < arr_size; i += num_threads) {
        now += x[i] * y[i];
    }
    sum[id] = now;
  }
  for (int i = 0; i < num_threads; i ++) {
    global_sum += sum[i];
  }
  free(sum);
  return global_sum;
}

下面是使用了reduction的代码:

double dotp_reduction_optimized(double* x, double* y, int arr_size) {
  double global_sum = 0.0;
#pragma omp parallel
  {
#pragma omp for reduction(+:global_sum)
    for (int i = 0; i < arr_size; i++)
      global_sum += x[i] * y[i];
  }
  return global_sum;
}

Part 2: Intro to multi-processing programming

这题貌似直接使用gcc-11会报错,可以改用gcc-9。或者把变量的定义放进server_utils.c 然后将 server_utils.h 中的变量加上extern

代码如下:

while (1) {
      client_socket_number = accept(*socket_number,
                                    (struct sockaddr *) &client_address,
                                    (socklen_t * ) & client_address_length);
      if (client_socket_number < 0) {
         perror("Error accepting socket");
         continue;
      }

      printf("Accepted connection from %s on port %d\n",
             inet_ntoa(client_address.sin_addr), client_address.sin_port);

      pid_t parent_pid = getpid();
#ifdef PROC
      // PART2 TASK: Implement forking
      /* YOUR CODE HERE */
      pid_t child_pid = fork();
      if (child_pid != 0) {
         // Kill child process if parent dies
         int r = prctl(PR_SET_PDEATHSIG, SIGTERM);

         /* YOUR CODE HERE */
         dispatch(client_socket_number);
         // Exit with code 1 when there was an error, 
         // or when the parent has been killed
         if (r == -1 || getppid() != parent_pid) {
            perror(0);
            exit(1);
         }

         /* YOUR CODE HERE */
         exit(EXIT_SUCCESS);
      }
#else
      dispatch(client_socket_number);
#endif
   }

  1. https://github.com/chibinz/CS110/blob/master/lab/lab10/dotp.c ↩︎

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值