source: trunk/CrypPlugins/KeySearcher/ExternalClient/Cryptool.cpp @ 2205

Last change on this file since 2205 was 2205, checked in by Sven Rech, 11 years ago

Added external client for other plattforms which can be used to support the keysearcher (client doesn't work yet)

File size: 11.0 KB
RevLine 
[2205]1/* ============================================================
2
3Copyright (c) 2009 Advanced Micro Devices, Inc.  All rights reserved.
4 
5Redistribution and use of this material is permitted under the following
6conditions:
7 
8Redistributions must retain the above copyright notice and all terms of this
9license.
10 
11In no event shall anyone redistributing or accessing or using this material
12commence or participate in any arbitration or legal action relating to this
13material against Advanced Micro Devices, Inc. or any copyright holders or
14contributors. The foregoing shall survive any expiration or termination of
15this license or any agreement or access or use related to this material.
16
17ANY BREACH OF ANY TERM OF THIS LICENSE SHALL RESULT IN THE IMMEDIATE REVOCATION
18OF ALL RIGHTS TO REDISTRIBUTE, ACCESS OR USE THIS MATERIAL.
19
20THIS MATERIAL IS PROVIDED BY ADVANCED MICRO DEVICES, INC. AND ANY COPYRIGHT
21HOLDERS AND CONTRIBUTORS "AS IS" IN ITS CURRENT CONDITION AND WITHOUT ANY
22REPRESENTATIONS, GUARANTEE, OR WARRANTY OF ANY KIND OR IN ANY WAY RELATED TO
23SUPPORT, INDEMNITY, ERROR FREE OR UNINTERRUPTED OPERA TION, OR THAT IT IS FREE
24FROM DEFECTS OR VIRUSES.  ALL OBLIGATIONS ARE HEREBY DISCLAIMED - WHETHER
25EXPRESS, IMPLIED, OR STATUTORY - INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED
26WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
27ACCURACY, COMPLETENESS, OPERABILITY, QUALITY OF SERVICE, OR NON-INFRINGEMENT.
28IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. OR ANY COPYRIGHT HOLDERS OR
29CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, PUNITIVE,
30EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, REVENUE, DATA, OR PROFITS; OR
32BUSINESS INTERRUPTION) HOWEVER CAUSED OR BASED ON ANY THEORY OF LIABILITY
33ARISING IN ANY WAY RELATED TO THIS MATERIAL, EVEN IF ADVISED OF THE POSSIBILITY
34OF SUCH DAMAGE. THE ENTIRE AND AGGREGATE LIABILITY OF ADVANCED MICRO DEVICES,
35INC. AND ANY COPYRIGHT HOLDERS AND CONTRIBUTORS SHALL NOT EXCEED TEN DOLLARS
36(US $10.00). ANYONE REDISTRIBUTING OR ACCESSING OR USING THIS MATERIAL ACCEPTS
37THIS ALLOCATION OF RISK AND AGREES TO RELEASE ADVANCED MICRO DEVICES, INC. AND
38ANY COPYRIGHT HOLDERS AND CONTRIBUTORS FROM ANY AND ALL LIABILITIES,
39OBLIGATIONS, CLAIMS, OR DEMANDS IN EXCESS OF TEN DOLLARS (US $10.00). THE
40FOREGOING ARE ESSENTIAL TERMS OF THIS LICENSE AND, IF ANY OF THESE TERMS ARE
41CONSTRUED AS UNENFORCEABLE, FAIL IN ESSENTIAL PURPOSE, OR BECOME VOID OR
42DETRIMENTAL TO ADVANCED MICRO DEVICES, INC. OR ANY COPYRIGHT HOLDERS OR
43CONTRIBUTORS FOR ANY REASON, THEN ALL RIGHTS TO REDISTRIBUTE, ACCESS OR USE
44THIS MATERIAL SHALL TERMINATE IMMEDIATELY. MOREOVER, THE FOREGOING SHALL
45SURVIVE ANY EXPIRATION OR TERMINATION OF THIS LICENSE OR ANY AGREEMENT OR
46ACCESS OR USE RELATED TO THIS MATERIAL.
47
48NOTICE IS HEREBY PROVIDED, AND BY REDISTRIBUTING OR ACCESSING OR USING THIS
49MATERIAL SUCH NOTICE IS ACKNOWLEDGED, THAT THIS MATERIAL MAY BE SUBJECT TO
50RESTRICTIONS UNDER THE LAWS AND REGULATIONS OF THE UNITED STATES OR OTHER
51COUNTRIES, WHICH INCLUDE BUT ARE NOT LIMITED TO, U.S. EXPORT CONTROL LAWS SUCH
52AS THE EXPORT ADMINISTRATION REGULATIONS AND NATIONAL SECURITY CONTROLS AS
53DEFINED THEREUNDER, AS WELL AS STATE DEPARTMENT CONTROLS UNDER THE U.S.
54MUNITIONS LIST. THIS MATERIAL MAY NOT BE USED, RELEASED, TRANSFERRED, IMPORTED,
55EXPORTED AND/OR RE-EXPORTED IN ANY MANNER PROHIBITED UNDER ANY APPLICABLE LAWS,
56INCLUDING U.S. EXPORT CONTROL LAWS REGARDING SPECIFICALLY DESIGNATED PERSONS,
57COUNTRIES AND NATIONALS OF COUNTRIES SUBJECT TO NATIONAL SECURITY CONTROLS.
58MOREOVER, THE FOREGOING SHALL SURVIVE ANY EXPIRATION OR TERMINATION OF ANY
59LICENSE OR AGREEMENT OR ACCESS OR USE RELATED TO THIS MATERIAL.
60
61NOTICE REGARDING THE U.S. GOVERNMENT AND DOD AGENCIES: This material is
62provided with "RESTRICTED RIGHTS" and/or "LIMITED RIGHTS" as applicable to
63computer software and technical data, respectively. Use, duplication,
64distribution or disclosure by the U.S. Government and/or DOD agencies is
65subject to the full extent of restrictions in all applicable regulations,
66including those found at FAR52.227 and DFARS252.227 et seq. and any successor
67regulations thereof. Use of this material by the U.S. Government and/or DOD
68agencies is acknowledgment of the proprietary rights of any copyright holders
69and contributors, including those of Advanced Micro Devices, Inc., as well as
70the provisions of FAR52.227-14 through 23 regarding privately developed and/or
71commercial computer software.
72
73This license forms the entire agreement regarding the subject matter hereof and
74supersedes all proposals and prior discussions and writings between the parties
75with respect thereto. This license does not affect any ownership, rights, title,
76or interest in, or relating to, this material. No terms of this license can be
77modified or waived, and no breach of this license can be excused, unless done
78so in a writing signed by all affected parties. Each term of this license is
79separately enforceable. If any term of this license is determined to be or
80becomes unenforceable or illegal, such term shall be reformed to the minimum
81extent necessary in order for this license to remain in effect in accordance
82with its terms as modified by such reformation. This license shall be governed
83by and construed in accordance with the laws of the State of Texas without
84regard to rules on conflicts of law of any state or jurisdiction or the United
85Nations Convention on the International Sale of Goods. All disputes arising out
86of this license shall be subject to the jurisdiction of the federal and state
87courts in Austin, Texas, and all defenses are hereby waived concerning personal
88jurisdiction and venue of these courts.
89
90============================================================ */
91
92
93//
94// Copyright (c) 2009 Advanced Micro Devices, Inc. All rights reserved.
95//
96
97#include <cstdio>
98#include <cstdlib>
99#include <iostream>
100#include <SDKFile.hpp>
101#include <SDKCommon.hpp>
102#include <SDKApplication.hpp>
103
104#define __NO_STD_VECTOR
105#define __NO_STD_STRING
106
107#include <CL/cl.hpp>
108
109#include "Job.h"
110int doOpenCLJob(const Job& j)
111{
112    cl_int err;
113
114    // Platform info
115    cl::vector<cl::Platform> platforms;
116    std::cout<<"HelloCL!\nGetting Platform Information\n";
117    err = cl::Platform::get(&platforms);
118    if(err != CL_SUCCESS)
119    {
120        std::cerr << "Platform::get() failed (" << err << ")" << std::endl;
121        return SDK_FAILURE;
122    }
123
124    cl::vector<cl::Platform>::iterator i;
125    if(platforms.size() > 0)
126    {
127        for(i = platforms.begin(); i != platforms.end(); ++i)
128        {
129            if(!strcmp((*i).getInfo<CL_PLATFORM_VENDOR>(&err).c_str(), "Advanced Micro Devices, Inc."))
130            {
131                break;
132            }
133        }
134    }
135    if(err != CL_SUCCESS)
136    {
137        std::cerr << "Platform::getInfo() failed (" << err << ")" << std::endl;
138        return SDK_FAILURE;
139    }
140
141    /*
142     * If we could find our platform, use it. Otherwise pass a NULL and get whatever the
143     * implementation thinks we should be using.
144     */
145
146    cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(*i)(), 0 };
147
148    std::cout<<"Creating a context AMD platform\n";
149    cl::Context context(CL_DEVICE_TYPE_CPU, cps, NULL, NULL, &err);
150    if (err != CL_SUCCESS) {
151        std::cerr << "Context::Context() failed (" << err << ")\n";
152        return SDK_FAILURE;
153    }
154
155    std::cout<<"Getting device info\n";
156    cl::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
157    if (err != CL_SUCCESS) {
158        std::cerr << "Context::getInfo() failed (" << err << ")\n";
159        return SDK_FAILURE;
160    }
161    if (devices.size() == 0) {
162        std::cerr << "No device available\n";
163        return SDK_FAILURE;
164    }
165
166    for(uint32_t i=0; i< devices.size(); ++i)
167    {
168        std::string out;
169        devices[i].getInfo(CL_DEVICE_NAME, &out);
170        printf("name: %s\n", out.c_str());
171        devices[i].getInfo(CL_DEVICE_VENDOR, &out);
172        printf("vendor: %s\n", out.c_str());
173        devices[i].getInfo(CL_DEVICE_OPENCL_C_VERSION, &out);
174        printf("version c: %s\n", out.c_str());
175    }
176    std::cout<<"compiling CL source\n";
177        cl::Program::Sources sources(1, std::make_pair(j.Src.c_str(), j.Src.length()));
178
179        cl::Program program = cl::Program(context, sources, &err);
180        if (err != CL_SUCCESS) {
181        std::cerr << "Program::Program() failed (" << err << ")\n";
182        return SDK_FAILURE;
183    }
184
185    err = program.build(devices);
186    if (err != CL_SUCCESS) {
187
188                if(err == CL_BUILD_PROGRAM_FAILURE)
189        {
190            cl::string str = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]);
191
192            std::cout << " \n\t\t\tBUILD LOG\n";
193            std::cout << " ************************************************\n";
194                        std::cout << str.c_str() << std::endl;
195            std::cout << " ************************************************\n";
196        }
197
198        std::cerr << "Program::build() failed (" << err << ")\n";
199        return SDK_FAILURE;
200    }
201
202    cl::Kernel kernel(program, "bruteforceKernel", &err);
203    if (err != CL_SUCCESS) {
204        std::cerr << "Kernel::Kernel() failed (" << err << ")\n";
205        return SDK_FAILURE;
206    }
207
208    cl::CommandQueue queue(context, devices[0], 0, &err);
209    if (err != CL_SUCCESS) {
210        std::cerr << "CommandQueue::CommandQueue() failed (" << err << ")\n";
211    }
212
213    // key
214    const char* strkey = "lol";
215
216    cl::Buffer keybuffer = cl::Buffer(context, CL_MEM_READ_ONLY, sizeof(strkey), NULL, &err);
217    if(err != CL_SUCCESS)
218    {
219        std::cerr << "Failed to allocate keybuffer(" << err << ")\n";
220        return SDK_FAILURE;
221    }
222
223    err = queue.enqueueWriteBuffer(keybuffer, 1, 0, sizeof(strkey), strkey);
224
225    if(err != CL_SUCCESS)
226    {
227        std::cerr << "Failed write to keybuffer(" << err << ")\n";
228        return SDK_FAILURE;
229    }
230
231    err = kernel.setArg(0, keybuffer);
232    if (err != CL_SUCCESS) {
233        std::cerr << "Kernel::setArg() failed (" << err << ")\n";
234        return SDK_FAILURE;
235    }
236
237    // results
238    cl::Buffer entropies = cl::Buffer(context, CL_MEM_WRITE_ONLY, sizeof(float)*20, NULL, &err);
239
240    if(err != CL_SUCCESS)
241    {
242        std::cerr << "Failed allocate to entropybuffer(" << err << ")\n";
243        return SDK_FAILURE;
244    }
245
246    err = kernel.setArg(1, entropies);
247
248    if (err != CL_SUCCESS) {
249        std::cerr << "Kernel::setArg() failed (" << err << ")\n";
250        return SDK_FAILURE;
251    }
252
253
254    std::cout<<"Running CL program\n";
255    err = queue.enqueueNDRangeKernel(
256        kernel, cl::NullRange, cl::NDRange(4, 4), cl::NDRange(2, 2)
257    );
258
259    if (err != CL_SUCCESS) {
260        std::cerr << "CommandQueue::enqueueNDRangeKernel()" \
261            " failed (" << err << ")\n";
262       return SDK_FAILURE;
263    }
264
265    err = queue.finish();
266    if (err != CL_SUCCESS) {
267        std::cerr << "Event::wait() failed (" << err << ")\n";
268        return SDK_FAILURE;
269    }
270
271    std::cout<<"Done\nPassed!\n";
272
273    float localEntropy[20] = {};
274
275    queue.enqueueReadBuffer(entropies, 1, 0, sizeof(float)*20, localEntropy);
276
277    for(int i=0; i<20; ++i)
278    {
279        printf("Entropy[%i]=%f\n", i, localEntropy[i]);
280    }
281    return SDK_SUCCESS;
282}
Note: See TracBrowser for help on using the repository browser.