武汉大学“多核架构及编程技术”精品课程课外作业

首页 申报信息 在线学习 教学改革 教学效果
课程导学 理论教学 课外作业 课程实验 教学录像 参考资源


作业2参考解答: 返回“课外作业”

#include <stdio.h>
#include <omp.h> 


void main()   
  {   
  int   K,n,i,j;   
  int   c[8][4][2]={{0,0,1,0,1,1,0,1},{1,0,1,1,0,0,0,1},{0,0,1,1,0,1,1,0},{1,0,0,1,0,0,1,1},{0,1,1,0,0,0,1,1},{0,1,0,0,1,0,1,1},{1,0,1,1,0,1,0,0},{0,1,1,1,1,0,0,0}};   
  int   ii,jj,s,m,b,a[1000][1000];
  int p=0;
  omp_set_num_threads(8);
  scanf("%d",&n);   
  if(n%2==1)
  	{
  		printf("oddy");
  		K=(5*n-1)/2; 
		#pragma omp parallel for schedule (dynamic,2) private (i) shared(K)
		for(i=0;i<n;i++)
			{
				#pragma omp parallel for private(j) shared(i)
			          for(j=0;j<n;j++)   
					a[i][j]=(K+j)%n*n+(K-j-1)%n+1;
				K--;
  			}
		for(i=0;i<n;i++)   
  			{
  				for(j=0;j<n;j++)   
  					printf("%d     ",a[i][j]);   
  					printf("n"); 
			}
  	}
  if(n%4==2)
  	{
		printf("the rest is two");
		p=n/2;
		#pragma omp parallel for schedule (dynamic, 2) private (ii)
		for(ii=0;ii<p;ii++)
			#pragma omp parallel for shared(ii) private(jj) shared(s)
  			for(jj=0;jj<p;jj++)
				{

  					if(ii==(p-1)/2)
						if(jj==(p-1)/2)m=1;
						else if(jj==(p-1)/2-1)m=2;   
  						else if(jj==(p-1)/2+1)m=4;   
  						else if(jj>(p-1)/2)m=6;   
  						else m=5;
						else if(jj==(p-1)/2&&ii==(p-1)/2-1)m=3;   
  						else if(ii>(p-1)/2)m=7;   
  						else m=0;
						b=((5*p-1)/2-ii+jj)%p*p+((5*p-1)/2-ii-jj-1)%p+1;   
  					for(s=0;s<4;s++)   
  						a[2*ii+c[m][s][0]][2*jj+c[m][s][1]]=4*b-3+s;   
  				}   
  		for(ii=0;ii<n;printf("n"),ii++)   
  			for(jj=0;jj<n;jj++)   
  				printf("%d     ",a[ii][jj]);  
  }
  if(n%4==0)
  	{
		printf("the rest is zero");
		s=0;
		#pragma omp parallel for schedule (dynamic, 2) private (i)
  		for(i=0;i<n;i++) 
			#pragma omp parallel for shared(i) private(j) shared(s)
  			for(j=0;j<n;j++)   
  				{
  					s++;   
  					if((i%4==0||i%4==3)&&(j%4==0||j%4==3)||(i%4==1||i%4==2)&&(j%4==1||j%4==2))   
  						a[i][j]=n*n+1-s;   
  					else a[i][j]=s;   
  				}   
  		for(i=0;i<n;i++)   
  			{
  				for(j=0;j<n;j++)   
  					printf("%d     ",a[i][j]);   
  					printf("n");   

  			}

  	}
}

Para obter informações mais completas sobre otimizações do compilador, consulte nosso aviso de otimização.