简体   繁体   English

C中的openmp并行递归函数

[英]openmp parallel recursive function in c

How do I parallelize, with openMP, the recursion below? 如何使用openMP并行化以下递归? Because I have a problem with my code that can be solved in this way. 因为我的代码有问题,可以通过这种方式解决。 I got this code from the following site: https://www.geeksforgeeks.org/write-ac-program-to-print-all-permutations-of-a-given-string/ 我从以下站点获得了此代码: https : //www.geeksforgeeks.org/write-ac-program-to-print-all-permutations-of-a-given-string/

Code: 码:

// C program to print all permutations with duplicates allowed
#include <stdio.h>
#include <string.h>

/* Function to swap values at two pointers */
void swap(char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void permute(char *a, int l, int r)
{
   int i;
   if (l == r)
     printf("%s\n", a);
   else
   {
       for (i = l; i <= r; i++)
       {
          swap((a+l), (a+i));
          permute(a, l+1, r);
          swap((a+l), (a+i)); //backtrack
       }
   }
}

/* Driver program to test above functions */
int main()
{
    char str[] = "ABC";
    int n = strlen(str);
    permute(str, 0, n-1);
    return 0;
}

There are at least two ways I can think of doing this. 我至少可以想到两种方法。 One is to parallelize over the permute function and the other is over the rank . 一种是在置换函数上并行化,另一种是在等级上并行化。

This answer uses the second method. 该答案使用第二种方法。 For n = strlen(str) the number of permutations (aka ranks) is n! 对于n = strlen(str) ,排列数(即秩)为n! . Eg for str = "ABCD" the number of ranks is 24 . 例如,对于str = "ABCD" ,等级数是24 Here is one method to do this over rank (based on this paper ): 这是一种超越等级的方法(基于本文 ):

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define SWAP(a,b) do{t=(a);(a)=(b);(b)=t;}while(0)

void get_permutation(int rank, int n, char *vec) {
    int t, q, r;
    if (n < 1) return;
    q = rank / n;
    r = rank % n;
    SWAP(vec[r], vec[n-1]);
    get_permutation(q, n-1, vec);
}

int main(int argc, char *argv[]) {
  char a[5] = "ABCD", t[5];

  #pragma omp parallel for private(t) schedule(dynamic)
  for (int r = 0; r < 24; ++r) {
    strcpy(t, a);
    get_permutation(r, 4, t);
    #pragma omp critical
    printf("%3d: %s\n", r, t);
  }
}

As long as get_permutation is slower than the output (in this case printf ) this method should win in performance. 只要get_permutation比输出慢(在本例中为printf ),此方法就应该可以提高性能。 I this will be true for a sufficiently large string length. 对于足够大的字符串长度,这将是正确的。

The output on my system is 我系统上的输出是

  3: BCAD
  6: DABC
  7: CABD
  9: DACB
  8: BDCA
 10: BADC
 11: BACD
 13: CDAB
 14: DBAC
 15: CBAD
 16: DCBA
  1: DCAB
 17: ACDB
 19: ACBD
 20: DBCA
 21: ADCB
 22: ABDC
 23: ABCD
 12: CBDA
  0: BCDA
 18: ADBC
  4: CDBA
  2: BDAC
  5: CADB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM