public static string Init(float[] inputs, uint span, uint blockSize) { _span = span; _block_size = blockSize; _area = _span * _span; _blocks_per_span = span / blockSize; _blockCount = _blocks_per_span * _blocks_per_span; d_indexRands = new IntPtr(); d_grid = new IntPtr(); _cudaArray = new CudaArray(); _gridProcs = new GridProcs(); _randoProcs = new RandoProcs(); var strRet = _cudaArray.ResetDevice(); strRet = strRet + _randoProcs.MakeGenerator32(SEED); strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_grid, _area); strRet = strRet + _cudaArray.CopyFloatsToDevice(inputs, d_grid, _area); strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_indexRands, _blockCount); return(strRet); }
public static string CopyToDevice(out GpuMatrix gmOut, GpuMatrix gmIn) { if (gmIn.DevHostState == DevHostState.DeviceNotAllocated) { gmOut = null; return("Device data pointer not allocated"); } var aa = new CudaArray(); var strRet = aa.CopyFloatsToDevice( gmIn.Matrix.Data.ToArray(), gmIn.DevPtr, (uint)gmIn.Matrix.Data.Length); if (!String.IsNullOrEmpty(strRet)) { gmOut = null; return(strRet); } gmOut = new GpuMatrix( matrix: gmIn.Matrix, devPtr: gmIn.DevPtr, devHostState: DevHostState.Synched); return(String.Empty); }
public static string Init(float[] inputs, uint span) { _span = span; _area = _span * _span; d_gridA = new IntPtr(); d_gridB = new IntPtr(); _cudaArray = new CudaArray(); _gridProcs = new GridProcs(); _randoProcs = new RandoProcs(); var strRet = _cudaArray.ResetDevice(); strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_gridA, _area); strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_gridB, _area); strRet = strRet + _cudaArray.CopyFloatsToDevice(inputs, d_gridA, _area); strRet = strRet + _cudaArray.CopyFloatsToDevice(inputs, d_gridB, _area); return(strRet); }
static string TestCopyFloatsDeviceToDevice() { string testName = "TestCopyFloatsDeviceToDevice"; uint arrayLen = 1000; var alist = Enumerable.Range(4, (int)arrayLen).Select(t => (float)t).ToArray(); var aa = new CudaArray(); IntPtr devDataA = new System.IntPtr(); IntPtr devDataB = new System.IntPtr(); var retlist = new float[(int)arrayLen]; try { var res = aa.ResetDevice(); res = res + aa.MallocFloatsOnDevice(ref devDataA, arrayLen); res = res + aa.MallocFloatsOnDevice(ref devDataB, arrayLen); res = res + aa.CopyFloatsToDevice(alist, devDataA, arrayLen); res = res + aa.CopyFloatsDeviceToDevice(devDataB, devDataA, arrayLen); res = res + aa.CopyFloatsFromDevice(retlist, devDataB, arrayLen); res = res + aa.ReleaseDevicePtr(devDataA); res = res + aa.ReleaseDevicePtr(devDataB); if (!alist.SequenceEqual(retlist)) { return(testName + " fail: sequences do not match"); } if (res != String.Empty) { return(testName + " fail: " + res); } return(testName + " pass"); } catch (Exception ex) { return(testName + " exception " + ex.Message); } finally { aa.ReleaseDevicePtr(devDataA); aa.ReleaseDevicePtr(devDataB); aa.ResetDevice(); } }
public static string Init(float[] temp_inputs, int[] flip_inputs, uint span, uint blockSize, int seed) { _span = span; _block_size = blockSize; _area = _span * _span; _blocks_per_span = span / blockSize; _blockCount = _blocks_per_span * _blocks_per_span; d_flipData = new IntPtr(); d_tempData = new IntPtr(); d_flipRands = new IntPtr(); d_indexRands = new IntPtr(); d_threshes = new IntPtr(); _cudaArray = new CudaArray(); _gridProcs = new GridProcs(); _randoProcs = new RandoProcs(); var strRet = _cudaArray.ResetDevice(); strRet = strRet + _randoProcs.MakeGenerator32(seed); strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_tempData, _area); strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_heatBlocks, _area / 1024); strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_flipData, _area); strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_indexRands, _blockCount); strRet = strRet + _cudaArray.MallocIntsOnDevice(ref d_flipRands, _blockCount); strRet = strRet + _cudaArray.MallocFloatsOnDevice(ref d_threshes, _allTempSteps); strRet = strRet + _cudaArray.CopyIntsToDevice(flip_inputs, d_flipData, _area); strRet = strRet + _cudaArray.CopyFloatsToDevice(temp_inputs, d_tempData, _area); var res9 = new int[_area]; strRet = strRet + _cudaArray.CopyIntsFromDevice(res9, d_flipData, _area); return(strRet); }
public static ProcResult UpdateE(int steps, float temp) { var strRet = String.Empty; float t2 = (float)(1.0 / (1.0 + Math.Exp(2 * temp))); float t4 = (float)(1.0 / (1.0 + Math.Exp(4 * temp))); float[] thresh = new float[10]; //thresh[1] = 1.0f; //thresh[3] = 1.0f; thresh[1] = 1.0f - t4; thresh[3] = 1.0f - t2; thresh[5] = 0.5f; thresh[7] = t2; thresh[9] = t4; strRet = strRet + _cudaArray.CopyFloatsToDevice(thresh, d_betas, 10); IntPtr dSrc; IntPtr dDest = IntPtr.Zero; _stopwatch.Reset(); _stopwatch.Start(); float energyTot = 0; int[] resB = new int[_area / 1024]; for (var s = 0; s < steps; s++) { if (_phase == 0) { dSrc = d_gridA; dDest = d_gridB; strRet = strRet + _randoProcs.MakeUniformRands(d_rands, _area); _phase = 1; } else { dSrc = d_gridB; dDest = d_gridA; _phase = 0; } strRet = strRet + _gridProcs.Runk_Ising_dg( destPtr: dDest, energyPtr: d_energy, srcPtr: dSrc, randPtr: d_rands, span: _span, alt: _phase, threshPtr: d_betas ); strRet = strRet + _cudaArray.RunBlockAddInts_32_Kernel( destPtr: d_energyBlocks, srcPtr: d_energy, span: _span ); strRet = strRet + _cudaArray.CopyIntsFromDevice(resB, d_energyBlocks, _area / 1024); energyTot += (float)resB.Sum() / (float)_area; } energyTot /= steps; int[] res = new int[_area]; strRet = strRet + _cudaArray.CopyIntsFromDevice(res, dDest, _area); _stopwatch.Stop(); var dRet = new Dictionary <string, object>(); dRet["Grid"] = new SimGrid <int>(name: "UpdateE", width: _span, height: _span, data: res); dRet["Energy"] = energyTot; return(new ProcResult(data: dRet, err: strRet, stepsCompleted: steps, time: _stopwatch.ElapsedMilliseconds)); }
public static ProcResult UpdateH(int steps, float qRate, float filpEnergy, float beta) { var strRet = String.Empty; _stopwatch.Reset(); _stopwatch.Start(); for (var s = 0; s < steps; s++) { var res9 = new int[_area]; strRet = strRet + _cudaArray.CopyIntsFromDevice(res9, d_flipData, _area); var bbs = FloatFuncs.Betas(_tempSteps, beta); strRet = strRet + _cudaArray.CopyFloatsToDevice(bbs, d_threshes, _allTempSteps); strRet = strRet + _randoProcs.MakeRandomInts(d_indexRands, _blockCount); strRet = strRet + _randoProcs.MakeUniformRands(d_flipRands, _blockCount); strRet = strRet + _gridProcs.Run_k_ThermoIsing_bp( temp_data: d_tempData, flip_data: d_flipData, index_rands: d_indexRands, flip_rands: d_flipRands, threshes: d_threshes, flip_energy: filpEnergy, block_size: _block_size, blocks_per_span: _blocks_per_span, q_rate: qRate); } _stopwatch.Stop(); var dRet = new Dictionary <string, object>(); float[] bhts = new float[_area / 1024]; strRet = strRet + _cudaArray.RunBlockAddFloats_32_Kernel( destPtr: d_heatBlocks, srcPtr: d_tempData, span: _span ); strRet = strRet + _cudaArray.CopyFloatsFromDevice(bhts, d_heatBlocks, _area / 1024); float tot = bhts.Sum(); tot /= _area; dRet["TotalHeat"] = tot; var tres = new float[_area]; strRet = strRet + _cudaArray.CopyFloatsFromDevice(tres, d_tempData, _area); dRet["ThermGrid"] = new SimGrid <float>(name: "Therms", width: _span, height: _span, data: tres); var fres = new int[_area]; strRet = strRet + _cudaArray.CopyIntsFromDevice(fres, d_flipData, _area); dRet["FlipGrid"] = new SimGrid <int>(name: "Flips", width: _span, height: _span, data: fres); return(new ProcResult(data: dRet, err: strRet, stepsCompleted: steps, time: _stopwatch.ElapsedMilliseconds)); }
public static ProcResult ProcIsingRb(int steps, float temp) { var strRet = String.Empty; float t2 = (float)(1.0 / (1.0 + Math.Exp(2 * temp))); float t4 = (float)(1.0 / (1.0 + Math.Exp(4 * temp))); float[] thresh = new float[5]; //thresh[0] = 1.0f; //thresh[1] = 1.0f; thresh[0] = 1.0f - t4; thresh[1] = 1.0f - t2; thresh[2] = 0.5f; thresh[3] = t2; thresh[4] = t4; strRet = strRet + _cudaArray.CopyFloatsToDevice(thresh, d_betas, 5); _stopwatch.Reset(); _stopwatch.Start(); for (var i = 0; i < steps; i++) { strRet = strRet + _randoProcs.MakeRandomInts(d_indexRands, _blockCount); strRet = strRet + _randoProcs.MakeUniformRands(d_tempRands, _blockCount); strRet = strRet + _gridProcs.Run_k_Ising_bp( destPtr: d_grid, energyPtr: d_energy, indexRandPtr: d_indexRands, tempRandPtr: d_tempRands, block_size: _block_size, blocks_per_span: _blocks_per_span, threshPtr: d_betas ); } strRet = strRet + _gridProcs.Runk_Energy4(d_energy, d_grid, _span); int[] mres = new int[_area / 1024]; strRet = strRet + _cudaArray.RunBlockAddInts_32_Kernel( destPtr: d_energyBlocks, srcPtr: d_energy, span: _span); strRet = strRet + _cudaArray.CopyIntsFromDevice(mres, d_energyBlocks, _area / 1024); float tot = mres.Sum(); tot /= _area; int[] res = new int[_area]; strRet = strRet + _cudaArray.CopyIntsFromDevice(res, d_grid, _area); _stopwatch.Stop(); //uint[] res2 = new uint[_blockCount]; //strRet = strRet + _cudaArray.CopyUIntsFromDevice(res2, d_rands, _blockCount); var dRet = new Dictionary <string, object>(); dRet["Grid"] = new SimGrid <int>(name: "Update", width: _span, height: _span, data: res); dRet["Energy"] = tot; return(new ProcResult(data: dRet, err: strRet, stepsCompleted: steps, time: _stopwatch.ElapsedMilliseconds)); }