Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C中的pthread库开发大型阵列的合并排序_C_Algorithm_Pthreads - Fatal编程技术网

使用C中的pthread库开发大型阵列的合并排序

使用C中的pthread库开发大型阵列的合并排序,c,algorithm,pthreads,C,Algorithm,Pthreads,在我的家庭作业中,我想根据下图所示的算法开发一个并行合并排序程序,对于任何数组大小N=2^M(20这应该在注释中,但我没有留下注释的名声 不能直接将命令行参数作为int,但可以使用以下语句将字符串转换为整数- intn=(int)strtol(M,(char**)NULL,10); 添加字符串.H< /Cult>库/< p>对不起,但你不是在某个时候问同样的问题吗?使用命名空间STD;< /C> > C++,而不是C@User1-St Tryatoi/atoi@User1 St,我没有告诉你把s

在我的家庭作业中,我想根据下图所示的算法开发一个并行合并排序程序,对于任何数组大小
N=2^M
(20这应该在注释中,但我没有留下注释的名声

不能直接将命令行参数作为
int
,但可以使用以下语句将字符串转换为整数-

intn=(int)strtol(M,(char**)NULL,10);


添加<代码>字符串.H< /Cult>库/< p>对不起,但你不是在某个时候问同样的问题吗?<代码>使用命名空间STD;< /C> > C++,而不是C@User1-St Try

atoi
/
atoi
@User1 St,我没有告诉你把
stoi
改成
stol
。我告诉你把
stoi
改成
atoi
stol
to
atol
@User1 St井,不是“还”,而是“仅”。
atoi
等应该代表“ASCII to int”(我想)。标准库中没有
stoi
./a.out  20  3 
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>

#define REP 3
#define MAX 50

//K
unsigned int K;

//input array
int* values;
unsigned int N;

//sorted array
int* sorted;

using namespace std;

//input of thread functions. position of the array for that thread.
typedef struct Arr {
   int low;
   int high;
} ArrayIndex;


void fillarray(int* data, unsigned int N)
{
    unsigned int i;
    for (i = 0; i < N; i++)
        data[i] = rand();
}



void *pms(void *a)
{
        ArrayIndex *pa = (ArrayIndex *)a;
        partition (values, pa->low,pa->high);
        return 0;
}



void partition(int *arr,int low,int high){

    int mid;
    if(low<high){
         mid=(low+high)/2;
         partition(arr,low,mid);
         partition(arr,mid+1,high);
         mergeSort(arr,low,mid,high);
    }
}

void mergeSort(int arr[],int low,int mid,int high){

    int i,m,k,l,temp[MAX];

    l=low;
    i=low;
    m=mid+1;

    while((l<=mid)&&(m<=high)){

         if(arr[l]<=arr[m]){
             temp[i]=arr[l];
             l++;
         }
         else{
             temp[i]=arr[m];
             m++;
         }
         i++;
    }

    if(l>mid){
         for(k=m;k<=high;k++){
             temp[i]=arr[k];
             i++;
         }
    }
    else{
         for(k=l;k<=mid;k++){
             temp[i]=arr[k];
             i++;
         }
    }

    for(k=low;k<=high;k++){
         arr[k]=temp[k];
    }
}

void check(){

    unsigned int i;
    for (i = 0; i <= N-2; i++)
        if (sorted[i] > sorted[i+1]) {
            printf("ERROR %d %d %d", i, sorted[i], sorted[i+1]);
            return;
        }

    printf("CORRECT\n");
}



int main( int argc, char *argv[] )
{
    int thread;
    // N = 2 ^ M
    long M = stol(argv[1]);
    N = (unsigned int) pow (2.0, M);

    // K
    K = (unsigned int) stoi(argv[2]) ;
    //test N and K:
    printf("N=%d K=%d\n", N, K);

    int thread_count = pow(2 ,K);
    ArrayIndex ai[thread_count];

    int i=0;
    for(i =0; i <thread_count; i++)
    {
        ai[i].low = i * (N / thread_count);
        ai[i].high = (i + 1) * (N / thread_count);
    }


    pthread_t* thread_handles;
    thread_handles = malloc(thread_count * sizeof(pthread_t));


    srand(time(0));

    time_t t1, t2;
    double dt; //t2-t1
    double tavg=0.0;

    //input array
    values = (int*) malloc ( sizeof(int) * N );

    int r; 
    for (r = 0; r < REP; r++)
    {
        //fill in the input array with random numbers
        fillarray(values, N);

        //t1
        t1 = time(0);

        //sort the array
        //parallel merge sort 


        thread=0;

        for(thread; thread<thread_count; thread++)
        {
            pthread_create(&thread_handles[thread], NULL, pms, &ai[thread]);
        }
        thread=0;
        for(thread=0; thread<thread_count; thread++)
        {
        pthread_join(thread_handles[thread], NULL);
        }


        //t2
            t2 = time(0);

        //t2-t1
            dt = t2 - t1;

        //average time
            tavg += (dt / REP);

        //check for correctness
            check();
    }

    printf ( "%g seconds\n", tavg );

    return 0;
}
user@sharifvm:~/the04a$ gcc -O2 -pthread  msort.c -lm
msort.c:45:6: warning: conflicting types for âpartitionâ [enabled by default]
 void partition(int *arr,int low,int high){
      ^
msort.c:39:3: note: previous implicit declaration of âpartitionâ was here
   partition (values, pa->low,pa->high);
   ^
msort.c:56:6: warning: conflicting types for âmergeSortâ [enabled by default]
 void mergeSort(int arr[],int low,int mid,int high){
      ^
msort.c:52:10: note: previous implicit declaration of âmergeSortâ was here
          mergeSort(arr,low,mid,high);
          ^
/tmp/ccCKc6qF.o: In function `main':
msort.c:(.text.startup+0x1e): undefined reference to `stol'
msort.c:(.text.startup+0x62): undefined reference to `stoi'
collect2: error: ld returned 1 exit status
user@sharifvm:~/the04a$