尝试在C语言中将OpenMP多线程实现到我的搜索器中以查找Collat​​z猜想并且失败

问题描述

让它与OpenMP一起正常工作是没有运气的。

使用多线程进行此操作时要注意的事项

  1. 将比较以正确的顺序运行到当前最长的运行,即{123,124,125}而不是{124,123,125},因为某些线程的完成速度比其他线程快。
  2. 我不知道如何将当前使用的do-while循环转换为等效的OMP FOR循环

我的一个想法是将每个线程的结果放入一个数组中,对它进行排序,然后一点一点地进行比较,再次实现这一点就没有运气了。

以下是当前有效的(顺序)代码:

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

void main() {

    unsigned __int64 a = 0;                  //running function value
    unsigned __int64 r = 0;                  //current starting value
    unsigned __int64 count = 0;              //current tree length

    unsigned __int64 champ_r = 0;            //number with the longest tree
    unsigned __int64 champ_count = 0;        //number of steps in the longest treetree

    do {
        count = 0;                           //reset tree length
        r++;                                 //set up the next starting value
        a = r;                                                                  
        do {
            if (a % 2 != 0) {
                a = a * 3 + 1;
                count++;
            } 
            if (a % 2 == 0) {
                a = a / 2;
                count++;
            }
        } while (a != 1);

        if (champ_count <= count) {
            champ_r = r;
            champ_count = count;
            printf("\nNumber of steps for %llu: %llu",champ_r,champ_count);
        }
    } while (1);
}

是的,它一直在运行,... 另外,我正在使用ULL,因为为什么不这样做,它可能会永远运行。

祝你好运!

PS。人们总是赞赏其他加快代码速度的改进。


EDIT1:切换为无符号__int64而不是无符号long long


EDIT2:这是我目前的工作方式,但是很慢

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

void main() {

    unsigned __int64 a = 0; //running function value
    unsigned __int64 r = 1; //current starting value
    unsigned __int64 count = 1; //current tree length

    unsigned __int64 champ_r = 0; //number with the longest tree
    unsigned __int64 champ_count = 0; //number of steps in the longest tree

    do {
        
#pragma omp parallel private(count) private(a)
        {
            r++;
            a = r;
            count = 1;
#pragma omp for
            for (int x = 2; x > 1; x++) {
                if (a % 2 != 0) {
                    a = a * 3 + 1;
                    x = a - 1;
                    count++;
                } else if (a % 2 == 0) {
                    a = a / 2;
                    x = a - 1;
                    count++;
                }
            }
#pragma omp critical
            {
                if (champ_count < count) {
                    champ_r = r;
                    champ_count = count;
                    printf("\nAnzahl der Schritte f\x81r %llu : %llu",champ_count);
                }
                count = 1;
            }
        }
    } while (1);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...