简体   繁体   English

并行化一个函数

[英]Parallelising a function

I'm trying to implement parallelism to this function I want it to take as many threads as possible, and write the results to a file.我正在尝试对此函数实现并行性,我希望它采用尽可能多的线程,并将结果写入文件。

The results need to be written in the file in the incrementing order so the first result needs to be written first the second second and so on.结果需要按递增顺序写入文件,因此第一个结果需要先写入第二个,依此类推。

The keyGen function is simply an MD5 of the integer m which is used as the start point for each chain. keyGen 函数只是整数 m 的 MD5,用作每个链的起点。 Reduction32 is a reduction function it takes the first 8 byte adds t and returns that value. Reduction32 是一个归约函数,它采用前 8 个字节加上 t 并返回该值。 When a chain reaches its endpoint it is stored in the binary file.当链到达其端点时,它会存储在二进制文件中。

Is there a smart way to make this parallel?有没有一种聪明的方法来使这种平行? without screwing up the order the endpoints are stored in?没有搞砸端点的存储顺序?

void tableGenerator32(uint32_t * text){
    int mMax = 33554432, lMax = 236;
    int m, t, i;
    uint16_t * temp;
    uint16_t * key, ep[2];
    uint32_t tp;
    FILE * write_ptr;
    write_ptr = fopen("table32bits.bin", "wb");
    for(m = 0; m < mMax ; m++){
        key = keyGen(m);
        for (t = 0; t < lMax; t++){
            keyschedule(key);
            temp = kasumi_enc(text);
            tp = reduction32(t,temp);
            temp[0]=tp>>16;
            temp[1]=tp;
            for(i=0; i < 8; i++){
                key[i]=temp[i%2];
            }
        }
        for(i=0;i<2;i++)
            ep[i] = key[i];

        fwrite(ep,sizeof(ep),1,write_ptr);
    }
    fclose(write_ptr);
}

The best way to parallelize the above function without facing concurrency issues is to create as many memory streams as many threads you wish to use and then divide the task into fractions, like if you have 4 threads,在不面临并发问题的情况下并行化上述函数的最佳方法是创建与您希望使用的线程数量一样多的内存流,然后将任务分成几部分,例如,如果您有 4 个线程,

  • one thread performs the task from 0 to mMax / 4一个线程执行从 0 到 mMax / 4 的任务
  • one thread performs the task from mMax / 4 to (mMax / 4) * 2一个线程执行从 mMax / 4 到 (mMax / 4) * 2 的任务
  • one thread performs the task from (mMax / 4) * 2 to (mMax / 4) * 3一个线程执行从 (mMax / 4) * 2 到 (mMax / 4) * 3 的任务
  • one thread performs the task from (mMax / 4) * 3 to (mMax / 4) * 4一个线程执行从 (mMax / 4) * 3 到 (mMax / 4) * 4 的任务

then you concatenate the result streams and write them into a file.然后连接结果流并将它们写入文件。

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

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