多线程合并排序编程

Multithreaded merge sort programming

本文关键字:编程 排序 合并 多线程      更新时间:2023-10-16

这是实验结果 - 原始经验值,多进程版本和多线程版本的运行时,比较了使用多程序和多线程之间合并排序编程的性能。比较在线程/进程的数量,数组中的元素数量下降低了。让我沮丧的是违反直觉的结果:随着线程/进程的数量增加,运行时增加; 多处理版本几乎比其他版本快10倍。正如我所期望的,多线程版本应该赢。我附上多线程源代码。请帮助我看看发生了什么。谢谢大家!

#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
// number of elements in array
// number of threads
int MAX;
int THREAD_MAX;
// array bellow is to store the right edge of a divided array
int index[20] = {0};
int p_i = 0;
using namespace std;
int *a;
int part = 0;
// merge function for merging two parts
void merge(int l1, int h1, int h2) {
    int count = h2 - l1 + 1;
    int sorted[count];
    int i = l1, k = h1 + 1, m = 0;
    while (i <= h1 && k <= h2) {
        if (a[i]<a[k])
            sorted[m++] = a[i++];
        else if (a[k]<a[i])
            sorted[m++] = a[k++];
        else if (a[i] == a[k]) {
            sorted[m++] = a[i++];
            sorted[m++] = a[k++];
        }
    }
    while (i <= h1)
        sorted[m++] = a[i++];
    while (k <= h2)
        sorted[m++] = a[k++];
    int arr_count = l1;
    for (i = 0; i<count; i++, l1++)
        a[l1] = sorted[i];
}
// merge sort function
void merge_sort(int low, int high) {
    // calculating mid point of array
    int mid = low + (high - low) / 2;
    if (low < high) {
        // calling first half
        merge_sort(low, mid);
        // calling second half
        merge_sort(mid + 1, high);
        // merging the two halves
        merge(low, mid, high);
    }
}
// thread function for multi-threading
void* merge_sort(void* arg) {
    int thread_part = part++;
    // calculating low and high
    int low = thread_part * (MAX / THREAD_MAX);
    int high = (thread_part + 1) * (MAX / THREAD_MAX) - 1;
    // allocate the rest part of original array to the last thread
    if(thread_part == THREAD_MAX -1){
        high = MAX - 1;
    }
    // store the right edge of each divided array
    index[++p_i] = high;
    // evaluating mid point
    int mid = low + (high - low) / 2;
    merge_sort(low, mid);
    merge_sort(mid + 1, high);
    merge(low, mid, high);
}
void isSorted(int len) {
    if (len == 1) {
        printf("Sorting Done Successfullyn");
        return;
    }
    int i;
    for (i = 1; i<len; i++) {
        if (a[i]<a[i - 1]) {
            printf("Sorting Not Donen");
            return;
        }
    }
    printf("Sorting Done Successfullyn");
    return;
}
// Driver Code
int main() {

    cout << "Enter No of elements of Array:";
    cin >> MAX;
    cout << "Enter No of Thread:";
    cin >> THREAD_MAX;

    // generating random values in array
    a = new int[MAX];
    srand(time(NULL));
    for (int i = 0; i < MAX; i++) {
        a[i] = rand();
    }
    // t1 and t2 for calculating time for merge sort
    clock_t t1 = clock();
    pthread_t threads[THREAD_MAX];
    // creating threads
    for (int i = 0; i < THREAD_MAX; i++) {
        pthread_create(&threads[i], NULL, merge_sort, (void*)NULL);
    }

    // joining all threads
    for (int i = 0; i < THREAD_MAX; i++) {
        pthread_join(threads[i], NULL);
    }
    // merging the final parts
    int p = 1;
    int mid, high;
    for(int q = 1; q < THREAD_MAX; q++) {   
        mid = index[p];
        p++;
        high = index[p];
        merge(0, mid, high);            
    }
    clock_t t2 = clock();
    cout << "Time taken: " << (double)(t2 - t1)/ CLOCKS_PER_SEC << endl;
    isSorted(MAX);
    // displaying sorted array
    /*cout << "Sorted array: ";
      for (int i = 0; i < MAX; i++)
      cout << a[i] << " ";*/
    return 0;
}

不明白为什么您的问题被投票。据我了解,您已经开发了两个具有相同操作的程序。您用multi process实施的一个,另一个是multi thread。首先,您需要知道multi processmulti thread之间的区别。我认为您在multi process版本中使用fork()。在multi process版本中,操作系统为每个过程创建单独的data segment,因此,当您使用fork() OS分配新的记忆空间时,为子过程分配新的存储空间,并将所有记忆从父母过程中复制到新创建的孩子的记忆位置。但是,如果多线程永远不会发生。因此,在多线程环境中的内存访问期间,如果您使用的是同步内存访问访问thread版本中的性能将受到严重影响,但在multi process版本中不会受到严重影响。