Ignore:
Timestamp:
Dec 16, 2010, 8:12:15 PM (11 years ago)
Author:
Sven Rech
Message:

keysearcher external client works now

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypPlugins/KeySearcher/ExternalClient/Cryptool.cpp

    r2212 r2215  
    8484        printf("version c: %s\n", out.c_str());
    8585    }
     86
     87    // results
     88    costs = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float)*subbatch, NULL, &err);
     89
     90    if(err != CL_SUCCESS)
     91    {
     92        std::cerr << "Failed allocate to costsbuffer(" << err << ")\n";
     93        throw new std::exception();
     94    }
     95   
     96    localCosts = new float[subbatch];
    8697}
    8798
     
    164175    }
    165176
    166     // results
    167     cl::Buffer costs = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float)*j.ResultSize, NULL, &err);
    168 
    169     if(err != CL_SUCCESS)
    170     {
    171         std::cerr << "Failed allocate to entropybuffer(" << err << ")\n";
    172         throw new std::exception();
    173     }
    174 
    175     //execute:
    176     std::cout<<"Running CL program\n";
    177     enqueueKernel(queue, j.Size, keybuffer, costs);
    178 
    179     err = queue.finish();
    180     if (err != CL_SUCCESS) {
    181         std::cerr << "Event::wait() failed (" << err << ")\n";
    182         throw new std::exception();
    183     }
    184 
    185     std::cout<<"Done Passed!\n";
    186 
    187     float* localCosts = new float[j.ResultSize];
    188 
    189     queue.enqueueReadBuffer(costs, 1, 0, sizeof(float)*j.ResultSize, localCosts);
    190 
    191     //check results:
    192     JobResult res;
    193177    res.ResultList.resize(j.ResultSize);
    194178    initTop(res.ResultList, j.LargerThen);
    195179
    196     for(int i=0; i<j.Size; ++i)
    197     {
    198 std::cout << localCosts[i] << std::endl;
    199         std::list<std::pair<float, int> >::iterator it = isInTop(res.ResultList, localCosts[i], j.LargerThen);
    200         if (it != res.ResultList.end())
    201             pushInTop(res.ResultList, it, localCosts[i], i);
    202     }
     180    //execute:
     181    std::cout<<"Running CL program with " << j.Size << " calculations!\n";
     182    enqueueKernel(queue, j.Size, keybuffer, costs, j);
     183
     184    std::cout<<"Done!\n";
    203185
    204186    return res;
    205187}
    206188
    207 const int subbatch = 256;
    208 
    209 void Cryptool::enqueueSubbatch(cl::CommandQueue& queue, cl::Buffer& keybuffer, cl::Buffer& costs, cl::NDRange offset, cl::NDRange worksize)
     189void Cryptool::enqueueSubbatch(cl::CommandQueue& queue, cl::Buffer& keybuffer, cl::Buffer& costs, int add, int length, const Job& j)
    210190{
    211191        cl_int err;
     
    223203        }
    224204
    225         err = kernel->setArg(2, 0);
     205        err = kernel->setArg(2, add);
    226206        if (err != CL_SUCCESS) {
    227207                std::cerr << "Kernel::setArg() failed (" << err << ")\n";
     
    229209        }
    230210
    231         err = queue.enqueueNDRangeKernel(*kernel, offset, worksize, cl::NullRange);
     211        err = queue.enqueueNDRangeKernel(*kernel, cl::NullRange, cl::NDRange(length), cl::NullRange);
    232212
    233213        if (err != CL_SUCCESS) {
     
    242222                throw new std::exception();
    243223        }
    244 }
    245 
    246 void Cryptool::enqueueKernel(cl::CommandQueue& queue, int size, cl::Buffer& keybuffer, cl::Buffer& costs)
     224
     225        queue.enqueueReadBuffer(costs, 1, 0, sizeof(float)*length, localCosts);
     226        err = queue.finish();
     227        if (err != CL_SUCCESS) {
     228                std::cerr << "Event::wait() failed (" << err << ")\n";
     229                throw new std::exception();
     230        }
     231
     232        //check results:
     233        for(int i=0; i<length; ++i)
     234        {
     235                //std::cout << localCosts[i] << std::endl;
     236                std::list<std::pair<float, int> >::iterator it = isInTop(res.ResultList, localCosts[i], j.LargerThen);
     237                if (it != res.ResultList.end())
     238                        pushInTop(res.ResultList, it, localCosts[i], i+add);
     239        }
     240}
     241
     242void Cryptool::enqueueKernel(cl::CommandQueue& queue, int size, cl::Buffer& keybuffer, cl::Buffer& costs, const Job& j)
    247243{
    248244    for (int i = 0; i < (size/subbatch); i++)
    249245    {
    250         enqueueSubbatch(queue, keybuffer, costs, cl::NDRange(i*subbatch), cl::NDRange(subbatch));
    251         std::cout << i << " von " << (size/subbatch) << std::endl;
     246        enqueueSubbatch(queue, keybuffer, costs, i*subbatch, subbatch, j);
    252247    }
    253248
     
    255250    if (remain != 0)
    256251    {
    257         enqueueSubbatch(queue, keybuffer, costs, cl::NDRange(size-remain), cl::NDRange(remain));
     252        enqueueSubbatch(queue, keybuffer, costs, size-remain, remain, j);
    258253    }
    259254}
Note: See TracChangeset for help on using the changeset viewer.