/// <summary> /// Creates a new surface from array memory. Allocates new array. /// </summary> /// <param name="kernel"></param> /// <param name="surfName"></param> /// <param name="flags"></param> /// <param name="format"></param> /// <param name="width">In elements</param> /// <param name="height">In elements</param> /// <param name="depth">In elements</param> /// <param name="numChannels"></param> /// <param name="arrayFlags"></param> public CudaSurface(CudaKernel kernel, string surfName, CUSurfRefSetFlags flags, CUArrayFormat format, SizeT width, SizeT height, SizeT depth, CudaArray3DNumChannels numChannels, CUDAArray3DFlags arrayFlags) { _surfref = new CUsurfref(); res = DriverAPINativeMethods.ModuleManagement.cuModuleGetSurfRef(ref _surfref, kernel.CUModule, surfName); Debug.WriteLine(String.Format("{0:G}, {1}: {2}, Surface name: {3}", DateTime.Now, "cuModuleGetSurfRef", res, surfName)); if (res != CUResult.Success) throw new CudaException(res); _flags = flags; _format = format; _height = height; _width = width; _depth = depth; _numChannels = (int)numChannels; _name = surfName; _module = kernel.CUModule; _cufunction = kernel.CUFunction; _channelSize = CudaHelperMethods.GetChannelSize(format); _dataSize = height * width * depth * _numChannels * _channelSize; _array = new CudaArray3D(format, width, height, depth, numChannels, arrayFlags); res = DriverAPINativeMethods.SurfaceReferenceManagement.cuSurfRefSetArray(_surfref, _array.CUArray, flags); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuSurfRefSetArray", res)); if (res != CUResult.Success) throw new CudaException(res); }
/// <summary> /// Returns a module handle<para/> /// Returns in \p *hmod the handle of the module that function \p hfunc /// is located in. The lifetime of the module corresponds to the lifetime of /// the context it was loaded in or until the module is explicitly unloaded.<para/> /// The CUDA runtime manages its own modules loaded into the primary context. /// If the handle returned by this API refers to a module loaded by the CUDA runtime, /// calling ::cuModuleUnload() on that module will result in undefined behavior. /// </summary> public CUmodule GetModule() { CUmodule temp = new CUmodule(); CUResult res = DriverAPINativeMethods.FunctionManagement.cuFuncGetModule(ref temp, this); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuFuncGetModule", res)); if (res != CUResult.Success) { throw new CudaException(res); } return(temp); }
public static extern CUResult cuModuleGetSurfRef(ref CUsurfref pSurfRef, CUmodule hmod, string name);
public static extern CUResult cuModuleGetTexRef(ref CUtexref pTexRef, CUmodule hmod, string name);
public static extern CUResult cuModuleGetGlobal_v2(ref CUdeviceptr dptr, ref SizeT bytes, CUmodule hmod, string name);
public static extern CUResult cuModuleGetFunction(ref CUfunction hfunc, CUmodule hmod, string name);
public static extern CUResult cuModuleUnload(CUmodule hmod);
public static extern CUResult cuModuleLoadFatBinary(ref CUmodule module, [In] byte[] fatCubin);
public static extern CUResult cuModuleLoadDataEx(ref CUmodule module, [In] byte[] image, uint numOptions, [In] CUJITOption[] options, [In, Out] IntPtr[] optionValues);
public static extern CUResult cuModuleLoadData(ref CUmodule module, [In] byte[] image);
public static extern CUResult cuModuleLoad(ref CUmodule module, string fname);
/// <summary> /// Creates a new surface from array memory. /// </summary> /// <param name="kernel"></param> /// <param name="surfName"></param> /// <param name="flags"></param> /// <param name="array"></param> public CudaSurface(CudaKernel kernel, string surfName, CUSurfRefSetFlags flags, CudaArray3D array) { _surfref = new CUsurfref(); res = DriverAPINativeMethods.ModuleManagement.cuModuleGetSurfRef(ref _surfref, kernel.CUModule, surfName); Debug.WriteLine(String.Format("{0:G}, {1}: {2}, Surface name: {3}", DateTime.Now, "cuModuleGetSurfRef", res, surfName)); if (res != CUResult.Success) throw new CudaException(res); _flags = flags; _format = array.Array3DDescriptor.Format; _height = array.Height; _width = array.Width; _depth = array.Depth; _numChannels = (int)array.Array3DDescriptor.NumChannels; _name = surfName; _module = kernel.CUModule; _cufunction = kernel.CUFunction; _channelSize = CudaHelperMethods.GetChannelSize(array.Array3DDescriptor.Format); _dataSize = array.Height * array.Width * array.Depth * array.Array3DDescriptor.NumChannels * _channelSize; _array = array; res = DriverAPINativeMethods.SurfaceReferenceManagement.cuSurfRefSetArray(_surfref, _array.CUArray, flags); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuSurfRefSetArray", res)); if (res != CUResult.Success) throw new CudaException(res); }
public GpuMathOperations() { this.cuBlas = new CudaBlas(); this.cudaContext = new CudaContext(); this.cuModule = cudaContext.LoadModulePTX(kernalFile); this.maxThreadPerBlockDim = (int)Math.Sqrt(this.cudaContext.GetDeviceInfo().MaxThreadsPerBlock); }
/// <summary> /// Creates a new mipmapped texture from array memory. Allocates a new mipmapped array. /// </summary> /// <param name="kernel"></param> /// <param name="texName"></param> /// <param name="addressMode0"></param> /// <param name="addressMode1"></param> /// <param name="addressMode2"></param> /// <param name="filterMode"></param> /// <param name="flags"></param> /// <param name="descriptor"></param> /// <param name="numMipmapLevels"></param> /// <param name="maxAniso"></param> /// <param name="mipmapFilterMode"></param> /// <param name="mipmapLevelBias"></param> /// <param name="minMipmapLevelClamp"></param> /// <param name="maxMipmapLevelClamp"></param> public CudaTextureMipmappedArray(CudaKernel kernel, string texName, CUAddressMode addressMode0, CUAddressMode addressMode1, CUAddressMode addressMode2, CUFilterMode filterMode, CUTexRefSetFlags flags, CUDAArray3DDescriptor descriptor, uint numMipmapLevels, uint maxAniso, CUFilterMode mipmapFilterMode, float mipmapLevelBias, float minMipmapLevelClamp, float maxMipmapLevelClamp) { _maxAniso = maxAniso; _mipmapFilterMode = mipmapFilterMode; _mipmapLevelBias = mipmapLevelBias; _minMipmapLevelClamp = minMipmapLevelClamp; _maxMipmapLevelClamp = maxMipmapLevelClamp; _texref = new CUtexref(); res = DriverAPINativeMethods.ModuleManagement.cuModuleGetTexRef(ref _texref, kernel.CUModule, texName); Debug.WriteLine(String.Format("{0:G}, {1}: {2}, Texture name: {3}", DateTime.Now, "cuModuleGetTexRef", res, texName)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetAddressMode(_texref, 0, addressMode0); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetAddressMode", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetAddressMode(_texref, 1, addressMode1); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetAddressMode", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetAddressMode(_texref, 2, addressMode2); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetAddressMode", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetFilterMode(_texref, filterMode); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetFilterMode", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetFlags(_texref, flags); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetFlags", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetFormat(_texref, descriptor.Format, (int)descriptor.NumChannels); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetFormat", res)); if (res != CUResult.Success) throw new CudaException(res); _filtermode = filterMode; _flags = flags; _addressMode0 = addressMode0; _addressMode1 = addressMode1; _addressMode2 = addressMode2; _arrayDescriptor = descriptor; _name = texName; _module = kernel.CUModule; _cufunction = kernel.CUFunction; _array = new CudaMipmappedArray(descriptor, numMipmapLevels); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetMipmappedArray(_texref, _array.CUMipmappedArray, CUTexRefSetArrayFlags.OverrideFormat); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetMipmappedArray", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetMaxAnisotropy(_texref, maxAniso); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetMaxAnisotropy", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetMipmapFilterMode(_texref, mipmapFilterMode); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetMipmapFilterMode", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetMipmapLevelBias(_texref, mipmapLevelBias); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetMipmapLevelBias", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetMipmapLevelClamp(_texref, minMipmapLevelClamp, maxMipmapLevelClamp); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetMipmapLevelClamp", res)); if (res != CUResult.Success) throw new CudaException(res); }
private void InitializeCUDA() { context = new CudaContext(CudaContext.GetMaxGflopsDevice(), graphicsDevice.ComPointer, CUCtxFlags.SchedAuto, CudaContext.DirectXVersion.D3D11); module = context.LoadModulePTX(@"Kernels\kernel.ptx"); kernelPositionWeightNoiseCube = new CudaKernel("position_weight_noise_cube", module, context); kernelNormalAmbient = new CudaKernel("normal_ambient", module, context); kernelMarchingCubesCases = new CudaKernel("marching_cubes_cases", module, context); kernelMarchingCubesVertices = new CudaKernel("marching_cubes_vertices", module, context); kernelPositionWeightNoiseCubeWarp = new CudaKernel("position_weight_noise_cube_warp", module, context); kernelPositionWeightFormula = new CudaKernel("position_weight_formula", module, context); prefixScan = new CUDAPrefixScan(module, context); }
/// <summary> /// Creates a new 2D texture from array memory. Allocates a new 2D array. /// </summary> /// <param name="kernel"></param> /// <param name="texName"></param> /// <param name="addressMode0"></param> /// <param name="addressMode1"></param> /// <param name="filterMode"></param> /// <param name="flags"></param> /// <param name="format"></param> /// <param name="height">In elements</param> /// <param name="width">In elements</param> /// <param name="numChannels">1,2 or 4</param> public CudaTextureArray2D(CudaKernel kernel, string texName, CUAddressMode addressMode0, CUAddressMode addressMode1, CUFilterMode filterMode, CUTexRefSetFlags flags, CUArrayFormat format, SizeT width, SizeT height, CudaArray2DNumChannels numChannels) { _texref = new CUtexref(); res = DriverAPINativeMethods.ModuleManagement.cuModuleGetTexRef(ref _texref, kernel.CUModule, texName); Debug.WriteLine(String.Format("{0:G}, {1}: {2}, Texture name: {3}", DateTime.Now, "cuModuleGetTexRef", res, texName)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetAddressMode(_texref, 0, addressMode0); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetAddressMode", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetAddressMode(_texref, 1, addressMode1); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetAddressMode", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetFilterMode(_texref, filterMode); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetFilterMode", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetFlags(_texref, flags); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetFlags", res)); if (res != CUResult.Success) throw new CudaException(res); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetFormat(_texref, format, (int)numChannels); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetFormat", res)); if (res != CUResult.Success) throw new CudaException(res); _filtermode = filterMode; _flags = flags; _addressMode0 = addressMode0; _addressMode1 = addressMode1; _format = format; _height = height; _width = width; _numChannels = (int)numChannels; _name = texName; _module = kernel.CUModule; _cufunction = kernel.CUFunction; _channelSize = CudaHelperMethods.GetChannelSize(format); _dataSize = height * width * _numChannels * _channelSize; _array = new CudaArray2D(format, width, height, numChannels); res = DriverAPINativeMethods.TextureReferenceManagement.cuTexRefSetArray(_texref, _array.CUArray, CUTexRefSetArrayFlags.OverrideFormat); Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuTexRefSetArray", res)); if (res != CUResult.Success) throw new CudaException(res); //res = DriverAPINativeMethods.ParameterManagement.cuParamSetTexRef(kernel.CUFunction, CUParameterTexRef.Default, _texref); //Debug.WriteLine("{0:G}, {1}: {2}", DateTime.Now, "cuParamSetTexRef", res); //if (res != CUResult.Success) throw new CudaException(res); }