I'm trying to parallelize the recursion in Karatsuba's polynomial multiply. But it's slower than without threading. What's problem I have this code:
int karatsubaMain(int size) { Polynom pol1(size),pol2(size); omp_set_num_threads(8); double start = omp_get_wtime(); int* result = mult(pol1.polynom,pol2.polynom,0,pol1.size); double end = omp_get_wtime(); printf("%f ", end - start); return 0; } int * mult(int*a, int *b,int start, int N){ int * c= new int[2*N-1]; if(N==1){ c[0]=a[start]*b[start]; return c; } int * t1= new int[N/2]; int * t2= new int[N/2]; int * cM,*cL,*cH; for(int i=0;i<N/2;i++){ t1[i]=a[start +i]+a[start + i + N/2]; t2[i]=b[start +i]+b[start + i + N/2 ]; } #pragma omp parallel shared(cM,cL,cH) { #pragma omp single nowait { #pragma omp task if(N > 4096) cM=mult(t1,t2,0,N/2); #pragma omp task if(N > 4096) cL=mult(a,b,0,N/2); #pragma omp task if(N > 4096) cH=mult(a,b,N/2,N/2); } #pragma omp taskwait } c[N-1]=0; for(int i=0;i<N-1;i++){ c[i]=cL[i]; c[N+i]=cH[i]; c[N/2+i]+=(cM[i]-(cL[i]+cH[i])); } delete []t1; delete []t2; delete []cM; delete []cL; delete []cH; return c; }