Advertisement
Guest User

Untitled

a guest
Mar 21st, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.88 KB | None | 0 0
  1. <PYTHON BEGINS>
  2.  
  3. import matplotlib.pyplot as plt, numpy as np
  4. #import matplotlib.animation as anim
  5. from matplotlib.widgets import Slider
  6. import time
  7. #from matplotlib import style
  8. import pyaudio
  9. import wave
  10. import threading
  11. from tkinter import filedialog as tkfd
  12. import tkinter as tk
  13. import concurrent.futures as cfthreading
  14. import ctypes, os
  15.  
  16. # see accelerator code
  17. acceleratorLocation = ""
  18. for a in os.path.dirname(__file__).split("\\")[0:-1]:
  19.     acceleratorLocation +=a+"\\"
  20. acceleratorLocation += "x64\\Debug\\accelerator.dll"
  21. print(acceleratorLocation)
  22. accelerator = ctypes.CDLL(acceleratorLocation)
  23.  
  24. print(accelerator.test2())
  25.  
  26. def checkError():
  27.     error = accelerator.getLastErrorCode()
  28.     lines = accelerator.getLastLineExecuted()
  29.     accelerator.resetErrors()
  30.     for i in range(1,error[0]+1):
  31.         if error[i]!=0:
  32.             print("Error occurred: " + str(error[i]) + " at cuda line " + str(lines[i]))
  33.  
  34. length = 69420
  35.  
  36. lengthFloat = ctypes.c_float * length
  37. accelerator.getLastErrorCode.restype = np.ctypeslib.ndpointer(dtype=ctypes.c_int,shape=(128,))
  38. accelerator.getLastLineExecuted.restype = np.ctypeslib.ndpointer(dtype=ctypes.c_int,shape=(128,))
  39. #accelerator.getLastErrorCode.restype = ctypes.c_int * 128
  40. accelerator.pycuAddOne.argtypes = [np.ctypeslib.ndpointer(dtype=ctypes.c_float,shape=(length,)),ctypes.c_int]
  41. accelerator.pycuAddOne.restypes = np.ctypeslib.ndpointer(dtype=ctypes.c_float,shape=(length,))
  42. #accelerator.pycuAddOne.restypes = ctypes.c_float * length
  43. accelerator.giveLotsOfZeroes.argtypes = [ctypes.c_int]
  44. #accelerator.giveLotsOfZeroes.restypes = np.ctypeslib.ndpointer(dtype=ctypes.c_int,shape=(length * (ctypes.sizeof(ctypes.c_float)/ctypes.sizeof(ctypes.c_float)),))
  45. accelerator.giveLotsOfZeroes.restypes = np.ctypeslib.ndpointer(dtype=ctypes.c_float,shape=(length,))
  46. accelerator.giveOneZero.restypes = ctypes.c_float
  47. accelerator.giveOneZeroPP.restypes = ctypes.c_float
  48. accelerator.pcGiveLotsOfZeroes.argtypes = [ctypes.c_int]
  49. #accelerator.pcGiveLotsOfZeroes.restypes = np.ctypeslib.ndpointer(dtype=ctypes.c_float,shape=(length,))
  50. accelerator.pcGiveLotsOfZeroes.restypes = ctypes.c_float
  51. accelerator.giveOneIntZero.restypes = ctypes.c_int
  52. accelerator.giveOneIntZeroPP.restypes = ctypes.c_int
  53.  
  54. testArray = np.array([0+0j]*length)
  55. testArray = np.array([np.real(testArray),np.imag(testArray)])
  56. print(testArray[0])
  57.  
  58.  
  59. accelerator.resetErrors()
  60. accelerator.pycuInitMemory(length)
  61. checkError()
  62. result = accelerator.pcGiveLotsOfZeroes(length)
  63. x= np.ctypeslib.as_array(np.array(result,dtype=np.float32),shape=(length,))
  64. print(type(result), str(result), type(x), type(np.array([1,2,3])))
  65. testArray[0] = x
  66. checkError()
  67.  
  68. print(x, testArray[0])
  69.  
  70. print("test pc ",np.array(accelerator.pcGiveLotsOfZeroes(length),dtype=np.float32))
  71.  
  72.  
  73. #tester = accelerator.giveLotsOfZeroes(length)#np.frombuffer(accelerator.giveLotsOfZeroes(length).getbuffer(),dtype=np.float32)
  74. #tester = np.frombuffer(ctypes.cast(accelerator.giveLotsOfZeroes(length),ctypes.POINTER(ctypes.c_float*length)).contents,dtype=np.float32)
  75. #tester =
  76. czero,cppzero = accelerator.giveOneZero(), accelerator.giveOneZeroPP()
  77. print(czero, cppzero)
  78. print(type(czero),type(cppzero))
  79. #print(accelerator.getLastErrorCode())
  80. #print(accelerator.getLastLineExecuted())
  81.  
  82. print("\n\n")
  83.  
  84. czeroInt,cppzeroInt = accelerator.giveOneIntZero(),accelerator.giveOneIntZeroPP()
  85. print(czeroInt,cppzeroInt)
  86. print(type(czeroInt),type(cppzeroInt))
  87.  
  88. accelerator.giveLotsOfZeroes.restypes = ctypes.POINTER(ctypes.c_float*length)
  89. result = accelerator.pcGiveLotsOfZeroes(length)
  90. print(result,type(result))
  91.  
  92. <PYTHON ENDS>
  93.  
  94. <kernal.cu BEGINS>
  95.  
  96.  
  97. #include "Python.h"
  98.  
  99. #include "cuda_runtime.h"
  100. #include "device_launch_parameters.h"
  101.  
  102. #include <stdio.h>
  103. #include <stdlib.h>
  104. //#include <string>
  105.  
  106. #include <cufft.h>
  107. #include <cufftXt.h>
  108. #include "helper_cuda.h"
  109. #include "helper_functions.h"
  110.  
  111. //#include "datatypes.h"
  112.  
  113. #define errorLogSize 128
  114.  
  115. extern "C" int testtern() { return 413; }
  116.  
  117. //IN BOTH ARRAYS, FIRST ELEMENT IS TO BE NOT TREATED AS AN ERROR, LASTERRORCODE[0] IS NUMBER OF ERRORS
  118. extern "C" int *lastErrorCodes = (int*)malloc(sizeof(int) * errorLogSize);
  119. extern "C" int *lastLinesExecuted = (int*)malloc(sizeof(int) * errorLogSize);
  120.  
  121. float* cuMemory;
  122.  
  123.  
  124.  
  125. extern "C" void resetErrorLog() {
  126.     for (unsigned int i = 0; i < errorLogSize; ++i) {
  127.         lastErrorCodes[i] = 0;
  128.         lastLinesExecuted[i] = 0;
  129.     }
  130. }
  131.  
  132. int convertError(cudaError_t input) {
  133.     switch (input) {
  134.     case cudaSuccess: return 0;
  135.     case cudaErrorMissingConfiguration: return 1;
  136.     case cudaErrorMemoryAllocation: return 2;
  137.     case cudaErrorInitializationError: return 3;
  138.     case cudaErrorLaunchFailure: return 4;
  139.     case cudaErrorLaunchTimeout: return 5;
  140.     case cudaErrorLaunchOutOfResources: return 6;
  141.     case cudaErrorInvalidDeviceFunction: return 7;
  142.     case cudaErrorInvalidConfiguration: return 8;
  143.     case cudaErrorInvalidDevice: return 9;
  144.     case cudaErrorInvalidValue: return 10;
  145.     case cudaErrorInvalidPitchValue: return 11;
  146.     case cudaErrorInvalidSymbol: return 12;
  147.     case cudaErrorUnmapBufferObjectFailed: return 13;
  148.     case cudaErrorInvalidDevicePointer: return 14;
  149.     case cudaErrorInvalidTexture: return 15;
  150.     case cudaErrorInvalidTextureBinding: return 16;
  151.     case cudaErrorInvalidChannelDescriptor: return 17;
  152.     case cudaErrorInvalidMemcpyDirection: return 18;
  153.     case cudaErrorInvalidFilterSetting: return 19;
  154.     case cudaErrorInvalidNormSetting: return 20;
  155.     case cudaErrorUnknown: return 21;
  156.     case cudaErrorInvalidResourceHandle: return 22;
  157.     case cudaErrorInsufficientDriver: return 23;
  158.     case cudaErrorNoDevice: return 24;
  159.     case cudaErrorSetOnActiveProcess: return 25;
  160.     case cudaErrorStartupFailure: return 26;
  161.     case cudaErrorInvalidPtx: return 27;
  162.     case cudaErrorNoKernelImageForDevice: return 28;
  163.     case cudaErrorJitCompilerNotFound: return 29;
  164.     }
  165. }
  166. void updateErrors(int lineCode) {
  167.     lastErrorCodes[0]++;
  168.     lastErrorCodes[lastErrorCodes[0]] = convertError(cudaGetLastError());
  169.     lastLinesExecuted[lastErrorCodes[0]] = lineCode;
  170. }
  171.  
  172. __global__
  173. void addOneToAll(float* list1) {
  174.     int i = blockIdx.x;
  175.     list1[i] = list1[i] + 1;
  176. }
  177.  
  178. extern "C" void initMemory(int length) {
  179.     cudaMallocManaged(&cuMemory, length * sizeof(float));
  180.     updateErrors(0);
  181. }
  182.  
  183.  
  184. extern "C" float* addOne(float* list1, int length) {
  185.  
  186.     std::cout << "\nLength is " << length << "\n\n";
  187.  
  188.     printf("\n(");
  189.     for (unsigned int i = 0; i < 6; ++i) {
  190.         std::cout << list1[i] << ", ";
  191.     }printf("... , ");
  192.     for (unsigned int i = length-6; i < length; ++i) {
  193.         std::cout << list1[i] << ", ";
  194.     }printf(")\n\n");
  195.  
  196.  
  197.     cudaMemcpy(cuMemory, &list1[0], length * sizeof(float), cudaMemcpyHostToDevice);
  198.     updateErrors(1);
  199.     addOneToAll <<< length, 1 >>> (list1);
  200.     updateErrors(2);
  201.     cudaMemcpy(&list1[0], cuMemory, length * sizeof(float), cudaMemcpyDeviceToHost);
  202.     updateErrors(3);
  203.    
  204.    
  205.    
  206.     for (unsigned int i = 0; i < length; ++i) {
  207.         list1[i]++;
  208.         if (i%2==0) list1[i] = list1[i]* 2;
  209.     }
  210.    
  211.     printf("\n(");
  212.     for (unsigned int i = 0; i < 12; ++i) {
  213.         std::cout << list1[i] << ", ";
  214.     }printf("... )\n\n");
  215.  
  216.     return list1;
  217. }
  218.  
  219. extern "C" float* giveLotsZeroes(int length) {
  220.     float* list2 = (float*)malloc(sizeof(float)*length);
  221.     for (unsigned int i = 0; i < length; i++) list2[i] = 0;
  222.     return list2;
  223. }
  224.  
  225. extern "C" float ppGiveOneZero() {
  226.     return 0.0f;
  227. }
  228.  
  229. extern "C" int ppGiveOneIntZero() {
  230.     return 0;
  231. }
  232.  
  233. /*
  234. extern "C" int ppInFloatOutFloat(float input) {
  235.     int integerVersion = reinterpret_cast<int>(&input)
  236.     return ;
  237. }*/
  238.  
  239. extern "C" char* ppGiveHelloWorld() {
  240.     return "Hello World";
  241. }
  242. <kernal.cu ENDS>
  243.  
  244. <chead.h BEGINS>
  245. #pragma once
  246.  
  247. extern int testtern();
  248. extern int *lastErrorCodes;
  249. extern int *lastLinesExecuted;
  250. extern void initMemory(int length);
  251. extern float* addOne(float* list1, int length);
  252. extern void resetErrorLog();
  253. extern float* giveLotsZeroes(int length);
  254. extern float ppGiveOneZero();
  255. extern int ppGiveOneIntZero();
  256. extern char* ppGiveHelloWorld();
  257.  
  258. __declspec(dllexport) int test();
  259.  
  260. __declspec(dllexport) int test2();
  261.  
  262. __declspec(dllexport) void pycuInitMemory(int length);
  263. __declspec(dllexport) float* pycuAddOne(float* list1, int length);
  264. __declspec(dllexport) int* getLastErrorCode();
  265. __declspec(dllexport) int* getLastLineExecuted();
  266. __declspec(dllexport) void resetErrors();
  267. __declspec(dllexport) float* giveLotsOfZeroes(int length);
  268. __declspec(dllexport) float giveOneZeroPP();
  269. __declspec(dllexport) float giveOneZero();
  270. __declspec(dllexport) float* pcGiveLotsOfZeroes(int length);
  271. __declspec(dllexport) int giveOneIntZero();
  272. __declspec(dllexport) int giveOneIntZeroPP();
  273. __declspec(dllexport) char* giveHelloWorldPP();
  274. __declspec(dllexport) char* giveHelloWorld();
  275.  
  276. <chead.h ENDS>
  277.  
  278. <Source.c BEGINS>
  279.  
  280. /*#define PY_SSSIZE_T_CLEAN*/
  281. #include "python.h"
  282. #include <stdlib.h>
  283. /*#include "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\include\Python.h"*/
  284. #include "chead.h"
  285.  
  286. /*extern int testtern();*/
  287.  
  288. int test() {
  289.     return testtern();
  290. }
  291.  
  292. int test2() {
  293.     return test() + 111;
  294. }
  295.  
  296.  
  297. void pycuInitMemory(int length) { initMemory(length); };
  298. float* pycuAddOne(float* list1, int length) { return addOne(list1, length); }
  299. int* getLastErrorCode() { return lastErrorCodes; };
  300. int* getLastLineExecuted() { return lastLinesExecuted; };
  301. void resetErrors() { resetErrorLog(); };
  302. float* giveLotsOfZeroes(int length) { return giveLotsZeroes(length); }
  303. float giveOneZeroPP() {
  304.     //char buf[100];
  305.     float x;
  306.     x = ppGiveOneZero();
  307.     /*printf("This is C speaking ");
  308.     printf(gcvt(x,12,buf));
  309.     printf("\n\n");*/
  310.     return x; }
  311. float giveOneZero() { return 0.0f; }
  312. int giveOneIntZero() { return 0; }
  313. int giveOneIntZeroPP() { return ppGiveOneIntZero(); }
  314.  
  315. float* pcGiveLotsOfZeroes(int length) {
  316.     float* list1 = (float*)malloc(length*sizeof(float));
  317.     int i = 0;
  318.     for (i; i < length; i++) {
  319.         list1[i] = 0;
  320.     }
  321.     return list1;
  322. }
  323.  
  324. char* giveHelloWorldPP() { return ppGiveHelloWorld(); }
  325. char* giveHelloWorld() { return "Hello World"; }
  326. <Source.c ENDS>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement