我需要从该顺序代码中并行(与openmp配合)斐波那契数列,以计算该序列的第10 5 项,但是我被困了3个星期,却没有一个好主意,有人对实现此目标有任何想法或技巧吗?
这是C中的顺序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100010
#define LEN 25001
char seq[MAX][LEN];
void add(int a, int b) {
int i, aux, s;
for (i = 0, aux = 0; seq[a][i] != '\0' && seq[b][i] != '\0'; i++) {
s = seq[a][i] + seq[b][i] + aux - '0' - '0';
aux = s / 10;
seq[a + 1][i] = s % 10 + '0';
}
while (seq[a][i] != '\0') {
s = seq[a][i] + aux - '0';
aux = s / 10;
seq[a + 1][i] = s % 10 + '0';
i++;
}
while (seq[b][i] != '\0') {
s = seq[b][i] + aux - '0';
aux = s / 10;
seq[a + 1][i] = s % 10 + '0';
i++;
}
if (aux != 0)
seq[a + 1][i++] = aux + '0';
seq[a + 1][i] = '\0';
}
int main() {
int n, i, len;
seq[0][0] = '0';
seq[0][1] = '\0';
seq[1][0] = '1';
seq[1][1] = '\0';
for (i = 2; i < MAX; i++)
add(i - 1, i - 2);
scanf("%d", &n);
len = strlen(seq[n]);
for (i = 0; i <= len - 1; i++)
printf("%c", seq[n][len - 1 - i]);
printf("\n");
fflush(stdout);
return 0;
}
答案 0 :(得分:0)
您已经知道,F(100000)是一个天文数字。为了计算该值,您必须对另外两个非常大的数字F(99999)和F(99998)求和。
这是我的提示:
您有两个数字,这些数字长(千)个位数,并且有N个处理器。您可以将加法分配到多个线程中。例如:
F(167) == 35600075545958458963222876581316753
F(168) == 57602132235424755886206198685365216
要计算F(169),必须在上面添加两个数字。但是,让我们将其视为4个独立的加法运算,每个加法运算均由9位数字组成。
(A) (B) (C) (D)
F(167) == 035600075 545958458 963222876 581316753
F(168) == 057602132 235424755 886206198 685365216
======================================================= +
93202207 781383213 1849429074 1266681969
^ ^
因此,现在我们有4个总结。 C
和D
其中两个具有进位运算符。所以我们只需要将每个左侧的结果调整+1
93202207 781383213 849429074 266681969
+1 +1
======================================================= +
F(169) == 93202207 781383214 849429075 266681969
因此,您计算Fib(100000)的算法如下所示。其中BigNumber
是代表数字的结构。您正在使用字符数组,这也是可以接受的。
void fib(int stop)
{
BigNumber f0 = 0;
BigNumber f1 = 1;
BigNumber f2 = 1;
BigNumber* pF0 = &f0;
BigNumber* pF1 = &f1;
BigNumber* pF2 = &f2;
for (int i = 2; i <= stop; i++)
{
ParallelAdd(pF0, pF1, pF2); // *pF2 = *pF1 + *pF0
// shift via pointers
pF0 = pF1;
pF1 = pF2;
pF2 = pF0;
}
Print(f2);
}
main()
{
fib(100000);
}
您的ParallelAdd
将通过指针f1和f0传递的数字分成N组,每组K个数字,其中N是您可用或想要使用的处理器数量。然后,每个处理器使用您已有的代码计算每个处理器的相加。在完成这N个操作后,扫描结果集以查看哪些加法结果包含K + 1位数字长的结果,然后如上所述使用+1逻辑进行调整。组合成一个字符串,然后退回到pF2
所引用的指针地址。
答案 1 :(得分:0)
与其尝试并行化bignum加法(这很棘手),还可以尝试并行计算多个项:
F(n+1) = F(n) + F(n-1)
F(n+2) = F(n+1) + F(n) = 2*F(n) + F(n-1)
F(n+3) = F(n+2) + F(n+1) = 2*F(n+1) + F(n) = 2*F(n) + 2*F(n-1) + F(n) = 3*F(n) + 2*F(n-1)
...
还请注意,您应该一次计算数字块:可以使用32位数组元素计算8或9个10进制的数字。
这是经过修改的版本,具有多项改进:
您应该能够轻松并行化它。
/* Parallelisable bignum Fibonacci computation by chqrlie */
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#if 0
/* 2500ms for fib(100000) */
#define DIGIT 10
#define NDIGIT 1
#define FMT "d"
typedef unsigned char digit_t;
#elif 1
/* 279ms for fib(100000) */
#define DIGIT 100000000
#define NDIGIT 8
#define FMT PRIu32
typedef uint32_t digit_t;
#else
/* 720ms for fib(100000) */
#define DIGIT 1000000000000000000
#define NDIGIT 18
#define FMT PRIu64
typedef uint64_t digit_t;
#endif
int add1(digit_t *c, const digit_t *a, int alen, const digit_t *b, int blen) {
digit_t aux, s;
int i;
/* assuming alen >= blen */
for (i = 0, aux = 0; i < alen; i++) {
s = a[i] + b[i] + aux;
aux = s / DIGIT;
c[i] = s % DIGIT;
}
if (aux != 0) {
c[i++] = (digit_t)aux;
}
return i;
}
int add2(digit_t *c, const digit_t *a, int alen, const digit_t *b, int blen) {
digit_t aux, s;
int i;
/* assuming alen >= blen */
for (i = 0, aux = 0; i < alen; i++) {
s = 2 * a[i] + b[i] + aux;
aux = s / DIGIT;
c[i] = s % DIGIT;
}
if (aux != 0) {
c[i++] = (digit_t)aux;
}
return i;
}
int add3(digit_t *c, const digit_t *a, int alen, const digit_t *b, int blen) {
digit_t aux, s;
int i;
/* assuming alen >= blen */
for (i = 0, aux = 0; i < alen; i++) {
s = 3 * a[i] + 2 * b[i] + aux;
aux = s / DIGIT;
c[i] = s % DIGIT;
}
if (aux != 0) {
c[i++] = (digit_t)aux;
}
return i;
}
int add4(digit_t *c, const digit_t *a, int alen, const digit_t *b, int blen) {
digit_t aux, s;
int i;
/* assuming alen >= blen */
for (i = 0, aux = 0; i < alen; i++) {
s = 5 * a[i] + 3 * b[i] + aux;
aux = s / DIGIT;
c[i] = s % DIGIT;
}
if (aux != 0) {
c[i++] = (digit_t)aux;
}
return i;
}
void printseq(const digit_t *s, int len) {
printf("%"FMT, s[len - 1]);
for (int i = 1; i < len; i++)
printf("%.*"FMT, NDIGIT, s[len - 1 - i]);
printf("\n");
}
int main(int argc, char *argv[]) {
int MIN, i, LEN, MAX;
if (argc > 1) {
MAX = MIN = strtol(argv[1], NULL, 0);
if (argc > 2)
MAX = strtol(argv[2], NULL, 0);
} else {
scanf("%d", &MIN);
MAX = MIN;
}
/* length if fib(n) is less than n*log10(phi)+2 */
LEN = (MAX * 20910ULL) / 100000 / NDIGIT + 2;
/* allocate 8 bignums */
int *slen = calloc(sizeof(*slen), 8);
digit_t (*seq)[LEN] = calloc(sizeof(*seq), 8);
if (slen == NULL || seq == NULL) {
fprintf(stderr, "memory allocation error\n");
return 1;
}
seq[0][0] = 0;
slen[0] = 1;
if (0 >= MIN) printseq(seq[0], slen[0]);
seq[1][0] = 1;
slen[1] = 1;
if (1 >= MIN) printseq(seq[1], slen[1]);
for (i = 2; i <= MAX && (MAX + 1 - i) % 4 != 0; i++) {
slen[i] = add1(seq[i], seq[i - 1], slen[i - 1], seq[i - 2], slen[i - 2]);
if (i >= MIN) printseq(seq[i], slen[i]);
}
for (; i <= MAX; i += 4) {
int im2 = (i - 2) & 7;
int im1 = (i - 1) & 7;
int i0 = (i + 0) & 7;
int i1 = (i + 1) & 7;
int i2 = (i + 2) & 7;
int i3 = (i + 3) & 7;
/* the next 4 calls can be parallelised */
slen[i0] = add1(seq[i0], seq[im1], slen[im1], seq[im2], slen[im2]);
slen[i1] = add2(seq[i1], seq[im1], slen[im1], seq[im2], slen[im2]);
slen[i2] = add3(seq[i2], seq[im1], slen[im1], seq[im2], slen[im2]);
slen[i3] = add4(seq[i3], seq[im1], slen[im1], seq[im2], slen[im2]);
/* the print calls must be called sequentially */
if (i + 0 >= MIN) printseq(seq[i0], slen[i0]);
if (i + 1 >= MIN) printseq(seq[i1], slen[i1]);
if (i + 2 >= MIN) printseq(seq[i2], slen[i2]);
if (i + 3 >= MIN) printseq(seq[i3], slen[i3]);
}
free(slen);
free(seq);
return 0;
}