Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <PYTHON BEGINS>
- import matplotlib.pyplot as plt, numpy as np
- #import matplotlib.animation as anim
- from matplotlib.widgets import Slider
- import time
- #from matplotlib import style
- import pyaudio
- import wave
- import threading
- from tkinter import filedialog as tkfd
- import tkinter as tk
- import concurrent.futures as cfthreading
- import ctypes, os
- # see accelerator code
- acceleratorLocation = ""
- for a in os.path.dirname(__file__).split("\\")[0:-1]:
- acceleratorLocation +=a+"\\"
- acceleratorLocation += "x64\\Debug\\accelerator.dll"
- print(acceleratorLocation)
- accelerator = ctypes.CDLL(acceleratorLocation)
- print(accelerator.test2())
- def checkError():
- error = accelerator.getLastErrorCode()
- lines = accelerator.getLastLineExecuted()
- accelerator.resetErrors()
- for i in range(1,error[0]+1):
- if error[i]!=0:
- print("Error occurred: " + str(error[i]) + " at cuda line " + str(lines[i]))
- length = 69420
- lengthFloat = ctypes.c_float * length
- accelerator.getLastErrorCode.restype = np.ctypeslib.ndpointer(dtype=ctypes.c_int,shape=(128,))
- accelerator.getLastLineExecuted.restype = np.ctypeslib.ndpointer(dtype=ctypes.c_int,shape=(128,))
- #accelerator.getLastErrorCode.restype = ctypes.c_int * 128
- accelerator.pycuAddOne.argtypes = [np.ctypeslib.ndpointer(dtype=ctypes.c_float,shape=(length,)),ctypes.c_int]
- accelerator.pycuAddOne.restypes = np.ctypeslib.ndpointer(dtype=ctypes.c_float,shape=(length,))
- #accelerator.pycuAddOne.restypes = ctypes.c_float * length
- accelerator.giveLotsOfZeroes.argtypes = [ctypes.c_int]
- #accelerator.giveLotsOfZeroes.restypes = np.ctypeslib.ndpointer(dtype=ctypes.c_int,shape=(length * (ctypes.sizeof(ctypes.c_float)/ctypes.sizeof(ctypes.c_float)),))
- accelerator.giveLotsOfZeroes.restypes = np.ctypeslib.ndpointer(dtype=ctypes.c_float,shape=(length,))
- accelerator.giveOneZero.restypes = ctypes.c_float
- accelerator.giveOneZeroPP.restypes = ctypes.c_float
- accelerator.pcGiveLotsOfZeroes.argtypes = [ctypes.c_int]
- #accelerator.pcGiveLotsOfZeroes.restypes = np.ctypeslib.ndpointer(dtype=ctypes.c_float,shape=(length,))
- accelerator.pcGiveLotsOfZeroes.restypes = ctypes.c_float
- accelerator.giveOneIntZero.restypes = ctypes.c_int
- accelerator.giveOneIntZeroPP.restypes = ctypes.c_int
- testArray = np.array([0+0j]*length)
- testArray = np.array([np.real(testArray),np.imag(testArray)])
- print(testArray[0])
- accelerator.resetErrors()
- accelerator.pycuInitMemory(length)
- checkError()
- result = accelerator.pcGiveLotsOfZeroes(length)
- x= np.ctypeslib.as_array(np.array(result,dtype=np.float32),shape=(length,))
- print(type(result), str(result), type(x), type(np.array([1,2,3])))
- testArray[0] = x
- checkError()
- print(x, testArray[0])
- print("test pc ",np.array(accelerator.pcGiveLotsOfZeroes(length),dtype=np.float32))
- #tester = accelerator.giveLotsOfZeroes(length)#np.frombuffer(accelerator.giveLotsOfZeroes(length).getbuffer(),dtype=np.float32)
- #tester = np.frombuffer(ctypes.cast(accelerator.giveLotsOfZeroes(length),ctypes.POINTER(ctypes.c_float*length)).contents,dtype=np.float32)
- #tester =
- czero,cppzero = accelerator.giveOneZero(), accelerator.giveOneZeroPP()
- print(czero, cppzero)
- print(type(czero),type(cppzero))
- #print(accelerator.getLastErrorCode())
- #print(accelerator.getLastLineExecuted())
- print("\n\n")
- czeroInt,cppzeroInt = accelerator.giveOneIntZero(),accelerator.giveOneIntZeroPP()
- print(czeroInt,cppzeroInt)
- print(type(czeroInt),type(cppzeroInt))
- accelerator.giveLotsOfZeroes.restypes = ctypes.POINTER(ctypes.c_float*length)
- result = accelerator.pcGiveLotsOfZeroes(length)
- print(result,type(result))
- <PYTHON ENDS>
- <kernal.cu BEGINS>
- #include "Python.h"
- #include "cuda_runtime.h"
- #include "device_launch_parameters.h"
- #include <stdio.h>
- #include <stdlib.h>
- //#include <string>
- #include <cufft.h>
- #include <cufftXt.h>
- #include "helper_cuda.h"
- #include "helper_functions.h"
- //#include "datatypes.h"
- #define errorLogSize 128
- extern "C" int testtern() { return 413; }
- //IN BOTH ARRAYS, FIRST ELEMENT IS TO BE NOT TREATED AS AN ERROR, LASTERRORCODE[0] IS NUMBER OF ERRORS
- extern "C" int *lastErrorCodes = (int*)malloc(sizeof(int) * errorLogSize);
- extern "C" int *lastLinesExecuted = (int*)malloc(sizeof(int) * errorLogSize);
- float* cuMemory;
- extern "C" void resetErrorLog() {
- for (unsigned int i = 0; i < errorLogSize; ++i) {
- lastErrorCodes[i] = 0;
- lastLinesExecuted[i] = 0;
- }
- }
- int convertError(cudaError_t input) {
- switch (input) {
- case cudaSuccess: return 0;
- case cudaErrorMissingConfiguration: return 1;
- case cudaErrorMemoryAllocation: return 2;
- case cudaErrorInitializationError: return 3;
- case cudaErrorLaunchFailure: return 4;
- case cudaErrorLaunchTimeout: return 5;
- case cudaErrorLaunchOutOfResources: return 6;
- case cudaErrorInvalidDeviceFunction: return 7;
- case cudaErrorInvalidConfiguration: return 8;
- case cudaErrorInvalidDevice: return 9;
- case cudaErrorInvalidValue: return 10;
- case cudaErrorInvalidPitchValue: return 11;
- case cudaErrorInvalidSymbol: return 12;
- case cudaErrorUnmapBufferObjectFailed: return 13;
- case cudaErrorInvalidDevicePointer: return 14;
- case cudaErrorInvalidTexture: return 15;
- case cudaErrorInvalidTextureBinding: return 16;
- case cudaErrorInvalidChannelDescriptor: return 17;
- case cudaErrorInvalidMemcpyDirection: return 18;
- case cudaErrorInvalidFilterSetting: return 19;
- case cudaErrorInvalidNormSetting: return 20;
- case cudaErrorUnknown: return 21;
- case cudaErrorInvalidResourceHandle: return 22;
- case cudaErrorInsufficientDriver: return 23;
- case cudaErrorNoDevice: return 24;
- case cudaErrorSetOnActiveProcess: return 25;
- case cudaErrorStartupFailure: return 26;
- case cudaErrorInvalidPtx: return 27;
- case cudaErrorNoKernelImageForDevice: return 28;
- case cudaErrorJitCompilerNotFound: return 29;
- }
- }
- void updateErrors(int lineCode) {
- lastErrorCodes[0]++;
- lastErrorCodes[lastErrorCodes[0]] = convertError(cudaGetLastError());
- lastLinesExecuted[lastErrorCodes[0]] = lineCode;
- }
- __global__
- void addOneToAll(float* list1) {
- int i = blockIdx.x;
- list1[i] = list1[i] + 1;
- }
- extern "C" void initMemory(int length) {
- cudaMallocManaged(&cuMemory, length * sizeof(float));
- updateErrors(0);
- }
- extern "C" float* addOne(float* list1, int length) {
- std::cout << "\nLength is " << length << "\n\n";
- printf("\n(");
- for (unsigned int i = 0; i < 6; ++i) {
- std::cout << list1[i] << ", ";
- }printf("... , ");
- for (unsigned int i = length-6; i < length; ++i) {
- std::cout << list1[i] << ", ";
- }printf(")\n\n");
- cudaMemcpy(cuMemory, &list1[0], length * sizeof(float), cudaMemcpyHostToDevice);
- updateErrors(1);
- addOneToAll <<< length, 1 >>> (list1);
- updateErrors(2);
- cudaMemcpy(&list1[0], cuMemory, length * sizeof(float), cudaMemcpyDeviceToHost);
- updateErrors(3);
- for (unsigned int i = 0; i < length; ++i) {
- list1[i]++;
- if (i%2==0) list1[i] = list1[i]* 2;
- }
- printf("\n(");
- for (unsigned int i = 0; i < 12; ++i) {
- std::cout << list1[i] << ", ";
- }printf("... )\n\n");
- return list1;
- }
- extern "C" float* giveLotsZeroes(int length) {
- float* list2 = (float*)malloc(sizeof(float)*length);
- for (unsigned int i = 0; i < length; i++) list2[i] = 0;
- return list2;
- }
- extern "C" float ppGiveOneZero() {
- return 0.0f;
- }
- extern "C" int ppGiveOneIntZero() {
- return 0;
- }
- /*
- extern "C" int ppInFloatOutFloat(float input) {
- int integerVersion = reinterpret_cast<int>(&input)
- return ;
- }*/
- extern "C" char* ppGiveHelloWorld() {
- return "Hello World";
- }
- <kernal.cu ENDS>
- <chead.h BEGINS>
- #pragma once
- extern int testtern();
- extern int *lastErrorCodes;
- extern int *lastLinesExecuted;
- extern void initMemory(int length);
- extern float* addOne(float* list1, int length);
- extern void resetErrorLog();
- extern float* giveLotsZeroes(int length);
- extern float ppGiveOneZero();
- extern int ppGiveOneIntZero();
- extern char* ppGiveHelloWorld();
- __declspec(dllexport) int test();
- __declspec(dllexport) int test2();
- __declspec(dllexport) void pycuInitMemory(int length);
- __declspec(dllexport) float* pycuAddOne(float* list1, int length);
- __declspec(dllexport) int* getLastErrorCode();
- __declspec(dllexport) int* getLastLineExecuted();
- __declspec(dllexport) void resetErrors();
- __declspec(dllexport) float* giveLotsOfZeroes(int length);
- __declspec(dllexport) float giveOneZeroPP();
- __declspec(dllexport) float giveOneZero();
- __declspec(dllexport) float* pcGiveLotsOfZeroes(int length);
- __declspec(dllexport) int giveOneIntZero();
- __declspec(dllexport) int giveOneIntZeroPP();
- __declspec(dllexport) char* giveHelloWorldPP();
- __declspec(dllexport) char* giveHelloWorld();
- <chead.h ENDS>
- <Source.c BEGINS>
- /*#define PY_SSSIZE_T_CLEAN*/
- #include "python.h"
- #include <stdlib.h>
- /*#include "D:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\include\Python.h"*/
- #include "chead.h"
- /*extern int testtern();*/
- int test() {
- return testtern();
- }
- int test2() {
- return test() + 111;
- }
- void pycuInitMemory(int length) { initMemory(length); };
- float* pycuAddOne(float* list1, int length) { return addOne(list1, length); }
- int* getLastErrorCode() { return lastErrorCodes; };
- int* getLastLineExecuted() { return lastLinesExecuted; };
- void resetErrors() { resetErrorLog(); };
- float* giveLotsOfZeroes(int length) { return giveLotsZeroes(length); }
- float giveOneZeroPP() {
- //char buf[100];
- float x;
- x = ppGiveOneZero();
- /*printf("This is C speaking ");
- printf(gcvt(x,12,buf));
- printf("\n\n");*/
- return x; }
- float giveOneZero() { return 0.0f; }
- int giveOneIntZero() { return 0; }
- int giveOneIntZeroPP() { return ppGiveOneIntZero(); }
- float* pcGiveLotsOfZeroes(int length) {
- float* list1 = (float*)malloc(length*sizeof(float));
- int i = 0;
- for (i; i < length; i++) {
- list1[i] = 0;
- }
- return list1;
- }
- char* giveHelloWorldPP() { return ppGiveHelloWorld(); }
- char* giveHelloWorld() { return "Hello World"; }
- <Source.c ENDS>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement