How do I parallelize, with openMP, the recursion below? 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/
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!
. Eg for str = "ABCD"
the number of ranks is 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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.