bool CostFunction::minimize() { real ftol = 1.0e-5; real minimum = 1e34; cout << "\tInner Loop Conjugate Gradient" << endl; conjugateGradient(currState, currGradient, ftol, minimum); return true; }
void cgComparisonTest(){ CL::TinyCL tiny(CL::DEVICE::GPU); SparseMatrix sMat("../res/bcsstk05.mtx"); std::cout << "Computing CG on matrix of dim: " << sMat.dim << std::endl; std::vector<float> b; for (int i = 0; i < sMat.dim; ++i) b.push_back(i); //Compare my kernel with the book kernel to make sure it's correct std::vector<float> localRes, myRes; //Measure elapsed time for my kernel and book kernel std::cout << "Book CG:\n"; std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); localRes = localConjGradSolve(sMat, b, tiny); std::chrono::high_resolution_clock::duration bookTime = std::chrono::high_resolution_clock::now() - start; std::cout << "------\nMy CG:\n"; start = std::chrono::high_resolution_clock::now(); myRes = conjugateGradient(sMat, b, tiny); std::chrono::high_resolution_clock::duration myTime = std::chrono::high_resolution_clock::now() - start; std::cout << "-----\nBook solve time: " << std::chrono::duration_cast<std::chrono::milliseconds>(bookTime).count() << "ms\n" << "My solve time: " << std::chrono::duration_cast<std::chrono::milliseconds>(myTime).count() << "ms\n" << "Time difference, mine - book: " << std::chrono::duration_cast<std::chrono::milliseconds>(myTime - bookTime).count() << "ms" << std::endl; //If the results are differ at a digit higher than the some minimal //error then my implementation is wrong float avgDiff = 0, maxDif = 1e-6; int nDifferent = 0; for (int i = 0; i < localRes.size(); ++i){ float diff = std::abs(localRes.at(i) - myRes.at(i)); if (diff > maxDif){ avgDiff += diff; ++nDifferent; } } if (nDifferent != 0) avgDiff /= nDifferent; std::cout << "# of values differing by more than " << std::scientific << maxDif << " : " << nDifferent << " of " << myRes.size() << "\nAverage difference between values: " << avgDiff << std::endl; }
void SimpleFluid::tests(){ //TODO: too high a grid dimension (128) crashed my video driver, what exactly was the //cause of the issue? Too much memory usage maybe? //Otherwise the fluid interaction matrix seems to solve really fast (<20 iterations) even with //my slower method, which is very good news //TODO: I've noticed that on occasion I the solution fails for some reason and I get back //1.#QNAN as the residual length so something bugs out somewhere, but I'm not sure where std::srand(std::time(NULL)); std::vector<float> b; for (int i = 0; i < interactionMat.dim; ++i) b.push_back(rand()); std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); std::vector<float> x = conjugateGradient(interactionMat, b, tiny); std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); std::cout << "Solving took: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl; }