简体   繁体   中英

GPU accelerated recursive function in C++

I'm quite new to C++(two days actually) and I would like to know if it is possible to do some parallelization with this code. I need this to be a hell lot faster as there are millions of iterations. From what I've understood so far it is not possible to parallelize, as the only for loop I use depends on the iteration before, which doesn't allow parallelization. Right? And if parallelization is not possible, how to optimize it otherwise so it gets faster. I was quite surprised as this only runs 3x faster than my original python code. (some said C++ is up to 100 to 400x faster than python)

If the VisualStudio 2015 project files are needed, please tell me..

If you run the application: You need to enter a sha1 hash and then tell the programm how many characters the base word had, so for example the word test: hash: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 length: 4

Thanks in advice

#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <iostream>
#include <cstring>
#include "..\crytoPP\sha.h"
#include "..\crytoPP\filters.h"
#include "..\crytoPP\hex.h"
#include "..\crytoPP\channels.h"

using namespace CryptoPP;
using namespace  std;

int found = 0;
int iteration = 0;
int length;
char source[] = "abcdefghijklmnopqrstuvwxyz";
string solution = " didn't match";
string base_hash;

string CHECK(string hash, int argc, char** argv);
void COMBINATIONS(string b, int length, int source_length, int argc, char** argv);

int main(int argc, char** argv)
{
    char *arr_ptr = &source[0];
    int source_length = strlen(arr_ptr);
    cout << "Please enter hash:";
    cin >> base_hash;
    cout << "Please enter length:";
    cin >> length;
    transform(base_hash.begin(), base_hash.end(), base_hash.begin(), ::toupper);
    COMBINATIONS("", ::length, source_length, argc - 1, argv + 1);
    system("PAUSE");
    return 0;
}

string CHECK(string hash, int argc, char** argv) {
    if (::found == 0) {
        iteration++;
        cout << iteration << endl;
        if (argc == 2 && argv[1] != NULL)
            hash = string(argv[1]);
        string s1;
        SHA1 sha1; SHA224 sha224; SHA256 sha256; SHA512 sha512;
        HashFilter f1(sha1, new HexEncoder(new StringSink(s1)));
        ChannelSwitch cs;
        cs.AddDefaultRoute(f1);
        StringSource ss(hash, true /*pumpAll*/, new Redirector(cs));
        cout << s1 << endl;
        if (s1 == ::base_hash) {
            ::found = 1;
            cout << " =" << hash << endl;
        }
        return s1;
    }  
}

void COMBINATIONS(string b, int length, int source_length, int argc, char** argv) {
    if (::found == 0) {
        if (length == 0) {
            CHECK(b, argc, argv);
        }
        else {
            for (int i = 0; i < source_length; i++) {
                COMBINATIONS(b + ::source[i], length -1, source_length, argc -1, argv + 1 );
            CHECK(b, argc - 1, argv + 1);
            }
        }
    }
}

The first thing you should try is to remove your output in every iteration as this does significantly reduce the performance of your program.

Right now you are invoking COMBINATIONS only once with an empty string b , but if you would create one thread for every starting string b of size 1 in main you can have eg 26 threads each solving an equally sized part of the problem. Yet, best would be to try to rewrite the COMBINATIONS function to be better suited for parallelism.

Moreover you are currently leaking memory every time you call CHECK which right now might not seem as much as a problem, however the longer the word you are looking for, the more memory your program will require. C++ requires you to manage the memory yourself, so you should at least free all the memory you allocated with new by using delete (to make sure it can be reused). Even better if you would try to reuse those objects you created as memory allocations are somewhat slow as well.

Last but not least please rethink the purpose of incrementing/decrementing argc and argv . Frankly I do not quite understand your intention there and it seems evil.

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.

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