internal static AnswerStruct GetAnswer() { using (var gpu = CudafyHost.GetDevice()) { gpu.LoadModule(CudafyTranslator.Cudafy()); var answer = new AnswerStruct[BlocksPerGrid]; ; var gpuAnswer = gpu.Allocate(answer); gpu.Launch(BlocksPerGrid, ThreadsPerBlock, GpuFindPathDistance, gpuAnswer); gpu.Synchronize(); gpu.CopyFromDevice(gpuAnswer, answer); gpu.FreeAll(); var bestDistance = float.MaxValue; var bestPermutation = 0L; for (var i = 0; i < BlocksPerGrid; i++) { if (answer[i].distance < bestDistance) { bestDistance = answer[i].distance; bestPermutation = answer[i].pathNo; } } return new AnswerStruct { distance = bestDistance, pathNo = bestPermutation }; } }
internal static AnswerStruct GetAnswer() { using (var gpu = CudafyHost.GetDevice()) { gpu.LoadModule(CudafyTranslator.Cudafy()); var answer = new AnswerStruct[BlocksPerGrid];; var gpuAnswer = gpu.Allocate(answer); gpu.Launch(BlocksPerGrid, ThreadsPerBlock, GpuFindPathDistance, gpuAnswer); gpu.Synchronize(); gpu.CopyFromDevice(gpuAnswer, answer); gpu.FreeAll(); var bestDistance = float.MaxValue; var bestPermutation = 0L; for (var i = 0; i < BlocksPerGrid; i++) { if (answer[i].distance < bestDistance) { bestDistance = answer[i].distance; bestPermutation = answer[i].pathNo; } } return(new AnswerStruct { distance = bestDistance, pathNo = bestPermutation }); } }
internal override Answer GetAnswer() { var stopWatchLoad = Stopwatch.StartNew(); using (var gpu = CudafyHost.GetDevice()) { var arch = gpu.GetDeviceProperties().Capability.GetArchitecture(); gpu.LoadModule(CudafyTranslator.Cudafy(ePlatform.x64, arch)); LoadTime = stopWatchLoad.ElapsedMilliseconds; var stopWatchRun = Stopwatch.StartNew(); var gpuLatLong = gpu.CopyToDevice(_latLong.ToArray()); var divisors = new long[_cities]; long divisor = _permutations; for (int city = _cities; city > 0; /* decrement in loop body */) { divisor /= city; city--; divisors[city] = divisor; } gpu.CopyToConstantMemory(divisors, gpuDivisors); var answer = new AnswerStruct[_blocksPerGrid];; var gpuAnswer = gpu.Allocate(answer); gpu.SafeLaunch(_blocksPerGrid, _threadsPerBlock, GpuFindPathDistance, _permutations, gpuLatLong, gpuAnswer); gpu.Synchronize(); gpu.CopyFromDevice(gpuAnswer, answer); gpu.FreeAll(); var bestDistance = float.MaxValue; var bestPermutation = 0L; for (var i = 0; i < _blocksPerGrid; i++) { if (answer[i].distance < bestDistance) { bestDistance = answer[i].distance; bestPermutation = answer[i].pathNo; } } return(new Answer { Distance = bestDistance, Permutation = bestPermutation, msLoadTime = LoadTime, msRunTime = stopWatchRun.ElapsedMilliseconds }); } }
internal override Answer GetAnswer() { var stopWatchLoad = Stopwatch.StartNew(); using (var gpu = CudafyHost.GetDevice()) { var arch = gpu.GetDeviceProperties().Capability.GetArchitecture(); gpu.LoadModule(CudafyTranslator.Cudafy(ePlatform.x64, arch)); LoadTime = stopWatchLoad.ElapsedMilliseconds; var stopWatchRun = Stopwatch.StartNew(); var gpuLatLong = gpu.CopyToDevice(_latLong.ToArray()); var answer = new AnswerStruct[_blocksPerGrid];; var gpuAnswer = gpu.Allocate(answer); gpu.SafeLaunch(_blocksPerGrid, _threadsPerBlock, GpuFindPathDistance, (int)_permutations, gpuLatLong, gpuAnswer); gpu.Synchronize(); gpu.CopyFromDevice(gpuAnswer, answer); gpu.FreeAll(); var bestDistance = float.MaxValue; var bestPermutation = 0; for (var i = 0; i < _blocksPerGrid; i++) { if (answer[i].distance < bestDistance) { bestDistance = answer[i].distance; bestPermutation = answer[i].pathNo; } } return(new AnswerBetter { Distance = bestDistance, Permutation = bestPermutation, msLoadTime = LoadTime, msRunTime = stopWatchRun.ElapsedMilliseconds }); } }
internal static Answer GpuTsp() { var stopWatchLoad = Stopwatch.StartNew(); using (var gpu = CudafyHost.GetDevice()) { gpu.LoadModule(CudafyTranslator.Cudafy()); LoadTime = stopWatchLoad.ElapsedMilliseconds; var stopWatchRun = Stopwatch.StartNew(); var gpuLatitudes = gpu.CopyToDevice(_latitudes.ToArray()); var gpuLongitudes = gpu.CopyToDevice(_longitudes.ToArray()); var answer = new AnswerStruct[_blocksPerGrid];; var gpuAnswer = gpu.Allocate(answer); gpu.SafeLaunch(_blocksPerGrid, _threadsPerBlock, GpuFindPathDistance, (int)_permutations, _cities, gpuLatitudes, gpuLongitudes, gpuAnswer); gpu.Synchronize(); gpu.CopyFromDevice(gpuAnswer, answer); var bestDistance = float.MaxValue; var bestPermutation = 0; for (var i = 0; i < _blocksPerGrid; i++) { if (answer[i].distance < bestDistance) { bestDistance = answer[i].distance; bestPermutation = answer[i].pathNo; } } return(new Answer { Distance = bestDistance, Permutation = bestPermutation, msLoadTime = LoadTime, msRunTime = stopWatchRun.ElapsedMilliseconds }); } }
static void RunTest(string threadInfo, AnswerStruct answer) { Console.WriteLine( string.Format("{0} {1,3} threads * {2,3} blocks returned.", threadInfo, answer.pathNo, answer.distance)); }
public static void GpuFindPathDistance(GThread thread, AnswerStruct[] answer) { var answerLocal = thread.AllocateShared<AnswerStruct>("ansL", ThreadsPerBlock); var bestDistance = thread.gridDim.x; var bestPermutation = thread.blockDim.x; var sum = 0; for (int i = 0; i < thread.blockDim.x; i++) sum += i * thread.threadIdx.x; answerLocal[thread.threadIdx.x].distance = bestDistance; answerLocal[thread.threadIdx.x].pathNo = bestPermutation; thread.SyncThreads(); if (thread.threadIdx.x == 0) { answer[thread.blockIdx.x] = answerLocal[0]; } }
AnswerStruct Calc(int a, int b, int c, int d) { AnswerStruct tmpResult = new AnswerStruct(); List<int[]> numbers = Algorithms.PermutationAndCombination<int>.GetPermutation(new int[] { a, b, c, d }); for (int i = 0; i < numbers.Count; i++) { for (int j = 0; j < allMethod.Count; j++) { float tmp = 0; if (allMethod[j][0] == '+') { tmp = numbers[i][0] + numbers[i][1]; } else if (allMethod[j][0] == '-') { tmp = numbers[i][0] - numbers[i][1]; } else if (allMethod[j][0] == 'x') { tmp = numbers[i][0] * numbers[i][1]; } else if (allMethod[j][0] == '/') { try { tmp = ((float)numbers[i][0] / numbers[i][1]); } catch (System.Exception) { continue; } } if (allMethod[j][1] == '+') { tmp = tmp + numbers[i][2]; } else if (allMethod[j][1] == '-') { tmp = tmp - numbers[i][2]; } else if (allMethod[j][1] == 'x') { tmp = tmp * numbers[i][2]; } else if (allMethod[j][1] == '/') { try { tmp = tmp / numbers[i][2]; } catch (System.Exception) { continue; } } if (allMethod[j][2] == '+') { tmp = tmp + numbers[i][3]; } else if (allMethod[j][2] == '-') { tmp = tmp - numbers[i][3]; } else if (allMethod[j][2] == 'x') { tmp = tmp * numbers[i][3]; } else if (allMethod[j][2] == '/') { try { tmp = tmp / numbers[i][3]; } catch (System.Exception) { continue; } } if (tmp == 24) { //string theValue = "(" + "(" + numbers[i][0] + allMethod[j][0] + numbers[i][1] + ")" + allMethod[j][1] + numbers[i][2] + ")" + allMethod[j][2] + numbers[i][3]; if (allMethod[j][0] == allMethod[j][1] && allMethod[j][1] == allMethod[j][2]) { return tmpResult; } tmpResult.has = true; tmpResult.numbers.Add(numbers[i][0]); tmpResult.numbers.Add(numbers[i][1]); tmpResult.numbers.Add(numbers[i][2]); tmpResult.numbers.Add(numbers[i][3]); tmpResult.operators.Add(allMethod[j][0].ToString()); tmpResult.operators.Add(allMethod[j][1].ToString()); tmpResult.operators.Add(allMethod[j][2].ToString()); return tmpResult; } } } return tmpResult; }
void GetQuestion() { AnswerStruct results = new AnswerStruct(); while (results.has == false) { int a = 0; int b = 0; int c = 0; int d = 0; if (StartSceneSetting.instance == null) { a = Random.Range(1, 10); b = Random.Range(1, 10); c = Random.Range(1, 10); d = Random.Range(1, 10); } else if (StartSceneSetting.instance.level == 0) { a = Random.Range(1, 10); b = Random.Range(1, 10); c = Random.Range(1, 10); d = Random.Range(1, 10); } else { a = Random.Range(1, 20); b = Random.Range(1, 20); c = Random.Range(1, 20); d = Random.Range(1, 20); while (a < 10 && b < 10 && c < 10 && d < 10) { a = Random.Range(1, 20); b = Random.Range(1, 20); c = Random.Range(1, 20); d = Random.Range(1, 20); } } results = Calc(a, b, c, d); if (results.has) { string q = "" + a + " " + "" + b + " " + "" + c + " " + "" + d + " "; Debug.Log(q); string answer = "(" + "(" + results.numbers[0] + results.operators[0] + results.numbers[1] + ")" + results.operators[1] + results.numbers[2] + ")" + results.operators[2] + results.numbers[3]; Debug.Log(answer); GameObject.Find("QuestionCardsPanel").transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>().text = a.ToString(); GameObject.Find("QuestionCardsPanel").transform.GetChild(1).GetChild(0).GetChild(0).GetComponent<Text>().text = b.ToString(); GameObject.Find("QuestionCardsPanel").transform.GetChild(2).GetChild(0).GetChild(0).GetComponent<Text>().text = c.ToString(); GameObject.Find("QuestionCardsPanel").transform.GetChild(3).GetChild(0).GetChild(0).GetComponent<Text>().text = d.ToString(); operator1.text = results.operators[0]; operator2.text = results.operators[1]; operator3.text = results.operators[2]; } } lastAnswer = results; }
void GetQuestion() { timeCounter.ResetAndStart(); AnswerStruct results = new AnswerStruct(); while (results.has == false) { int a = 0; int b = 0; int c = 0; int d = 0; Random.seed = System.Environment.TickCount; Debug.Log("Random.seed = " + Random.seed); if (isDebugRandom) { Random.seed = debugNum; } if (StartSceneSetting.instance == null) { a = Random.Range(1, 10); b = Random.Range(1, 10); c = Random.Range(1, 10); d = Random.Range(1, 10); } else if (StartSceneSetting.instance.level == 0) { a = Random.Range(1, 10); b = Random.Range(1, 10); c = Random.Range(1, 10); d = Random.Range(1, 10); } else { a = Random.Range(1, 20); b = Random.Range(1, 20); c = Random.Range(1, 20); d = Random.Range(1, 20); while (a < 10 && b < 10 && c < 10 && d < 10) { a = Random.Range(1, 20); b = Random.Range(1, 20); c = Random.Range(1, 20); d = Random.Range(1, 20); } } results = Calc(a, b, c, d); if (results.has) { string q = "" + a + " " + "" + b + " " + "" + c + " " + "" + d + " "; Debug.Log(q); string sz = "(" + "(" + results.numbers[0] + results.operators[0] + results.numbers[1] + ")" + results.operators[1] + results.numbers[2] + ")" + results.operators[2] + results.numbers[3]; Debug.Log(sz); for (int i = 0; i < 4; i++) { questionButtons[i].transform.GetChild(0).GetComponent<Text>().text = results.numbers[i].ToString(); } answer = results; } } }