/* * Advanced Simulation Library * * Copyright 2015 Avtech Scientific * * * This file is part of Advanced Simulation Library (ASL). * * ASL is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, version 3 of the License. * * ASL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with ASL. If not, see . * */ #include "aclReductionAlgGenerator.h" #include "aclVectorOfElementsOperations.h" #include #include #include #include namespace acl { inline const unsigned int nLocalGPU(unsigned int length, unsigned int nGroups) { return std::max(1u,std::min(8u, length/nGroups)); } inline const unsigned int nLocalCPU(unsigned int length, unsigned int nGroups) { return 1; } unsigned int getLPerUnit(unsigned int length, unsigned int nUnits) { return ceil((double)length/nUnits); } unsigned int getNSaturatedUnits(unsigned int length, unsigned int nUnits) { return length/getLPerUnit(length,nUnits); } unsigned int getLLastUnit(unsigned int length, unsigned int nUnits) { return length % getLPerUnit(length,nUnits); } template class GroupReduction { }; template class GroupReduction { public: static void compute(std::vector> v, unsigned int l, asl::AVec & res) { for(unsigned int i(0); i & vv(v[i]); ResType r(vv[0]); for(unsigned int j(1); j::compute(std::vector> v, // asl::AVec & res); // template void GroupReduction::compute(std::vector> v, // asl::AVec & res); template class GroupReduction { public: static void compute(std::vector> v, unsigned int l, asl::AVec & res) { for(unsigned int i(0); i & vv(v[i]); ResType r(vv[0]); for(unsigned int j(1); j::compute(std::vector> v, // asl::AVec & res); // template void GroupReduction::compute(std::vector> v, // asl::AVec & res); template class GroupReduction { public: static void compute(std::vector> v, unsigned int l, asl::AVec & res) { for(unsigned int i(0); i & vv(v[i]); ResType r(vv[0]); for(unsigned int j(1); j::compute(std::vector> v, // asl::AVec & res); // template void GroupReduction::compute(std::vector> v, // asl::AVec & res); template class GroupReduction { public: static void compute(std::vector> v, unsigned int l, asl::AVec & res) { for(unsigned int i(0); i & vv(v[i]); ResType r(vv[0]); for(unsigned int j(1); j::compute(std::vector> v, // asl::AVec & res); // template void GroupReduction::compute(std::vector> v, // asl::AVec & res); template void ReductionAlgGenerator::compute() { if(kernel) kernel->compute(); unsigned int nC(ve.size()); unsigned int veLength(ve[0]->getSize()); unsigned int nSaturatedUnits(getNSaturatedUnits(veLength, nGroups*nLocal)); unsigned int l(std::min(nSaturatedUnits+1,nGroups*nLocal)); for (unsigned int i(0); i < nC; ++i) acl::copy(groupResACL[i], groupRes[i]); GroupReduction::compute(groupRes, l, res.v()); }; template void ReductionAlgGenerator::compute(); template void ReductionAlgGenerator::compute(); template void ReductionAlgGenerator::compute(); template void ReductionAlgGenerator::compute(); template void ReductionAlgGenerator::compute(); template void ReductionAlgGenerator::compute(); template void ReductionAlgGenerator::compute(); template void ReductionAlgGenerator::compute(); template void generateKernelCPU(VectorOfElements ve, VectorOfElements groupResACL, Kernel & k) { auto nGroups(k.getGroupsNumber()); int veLength(ve[0]->getSize()); unsigned int nLocal(nLocalCPU(veLength,nGroups)); unsigned int gSize(std::max(k.getSize(),nLocal)); auto type(getElementType(ve)); auto typeI(TYPE_SELECT[type]); auto lVal(acl::generateVEPrivateVariable(ve.size(),type)); auto counter(acl::generateVEPrivateVariable(1,typeI)); auto counterEnd(acl::generateVEPrivateVariable(1,typeI)); int lPerGroup(getLPerUnit(veLength,nGroups)); int lLastGroup(getLLastUnit(veLength, nGroups)); int nSaturatedGroups(getNSaturatedUnits(veLength, nGroups)); k<<(counterEnd = select(generateVEConstant(lPerGroup), generateVEConstant(lLastGroup), generateVEGroupID()==nSaturatedGroups, type)); k<<(lVal = select(excerpt(ve,lPerGroup*generateVEGroupID()), counterEnd>0,type)); // this line ensuers that the conputations will take place on the 1st item only k<<(counterEnd = select(counterEnd, generateVEIndex(gSize)==0, type)); vector body; body<<(GroupReduction:: op(lVal,excerpt(ve, int(lPerGroup)*generateVEGroupID() + counter),type)); auto loop(elementOperators::forLoop((counter = generateVEConstant(1))[0], (counter < counterEnd)[0], (counter += generateVEConstant(1))[0], body)); k.addExpression(loop); k<<(excerpt(groupResACL,generateVEGroupID()) = lVal); } template void generateKernelGPU(VectorOfElements ve, VectorOfElements groupResACL, Kernel & k) { auto nGroups(k.getGroupsNumber()); int veLength(ve[0]->getSize()); unsigned int nLocal(nLocalGPU(veLength,nGroups)); unsigned int gSize(std::max(k.getSize(),nLocal)); auto type(getElementType(ve)); auto typeI(TYPE_SELECT[type]); auto lVal(acl::generateVEPrivateVariable(ve.size(),type)); auto counter(acl::generateVEPrivateVariable(1,typeI)); auto counterEnd(acl::generateVEPrivateVariable(1,typeI)); auto nUnits(nGroups*nLocal); int lPerUnit(getLPerUnit(veLength,nUnits)); int lLastUnit(getLLastUnit(veLength, nUnits)); unsigned int nSaturatedUnits(getNSaturatedUnits(veLength, nUnits)); auto unitID(generateVEGroupID()*nLocal +generateVEIndex()); if(nSaturatedUnits0,type)); vector body; body<<(GroupReduction:: op(lVal,excerpt(ve, lPerUnit*unitID + counter),type)); auto loop(elementOperators::forLoop((counter = generateVEConstant(1))[0], (counter < counterEnd)[0], (counter += generateVEConstant(1))[0], body)); k.addExpression(loop); k<<(excerpt(groupResACL,unitID) = lVal); } template void ReductionAlgGenerator::generateAlg(Kernel & k) { if(!k.getConfiguration().local) asl::errorMessage("ReductionAlgGenerator::generateAlg: The kernel should be local"); nGroups= k.getGroupsNumber(); unsigned int veLength(ve[0]->getSize()); nLocal=(getDeviceType(ve[0]->getQueue())== CL_DEVICE_TYPE_CPU ? nLocalCPU(veLength,nGroups) : nLocalGPU(veLength,nGroups)); for(unsigned int i(0); i(nGroups*nLocal); copy(generateVEData(nGroups*nLocal,ve.size()),groupResACL); switch (getDeviceType(ve[0]->getQueue())) { case CL_DEVICE_TYPE_CPU: generateKernelCPU(ve,groupResACL,k); k.setup(); break; case CL_DEVICE_TYPE_GPU: generateKernelGPU(ve,groupResACL,k); k.setup(); break; default: errorMessage("ReductionAlgGenerator: device type " + numToStr(getDeviceType(k.getQueue())) + " is unknown!"); } } template void ReductionAlgGenerator::generateAlg(Kernel & k); template void ReductionAlgGenerator::generateAlg(Kernel & k); template void ReductionAlgGenerator::generateAlg(Kernel & k); template void ReductionAlgGenerator::generateAlg(Kernel & k); template void ReductionAlgGenerator::generateAlg(Kernel & k); template void ReductionAlgGenerator::generateAlg(Kernel & k); template void ReductionAlgGenerator::generateAlg(Kernel & k); template void ReductionAlgGenerator::generateAlg(Kernel & k); template void ReductionAlgGenerator::generateAlg() { KernelConfiguration kConf(KERNEL_BASIC); kConf.local = true; kernel=std::make_shared(kConf); unsigned int groupsNumber = getNComputeUnits(ve[0]->getQueue()); kernel->setGroupsNumber(groupsNumber); generateAlg(*kernel); }; template void ReductionAlgGenerator::generateAlg(); template void ReductionAlgGenerator::generateAlg(); template void ReductionAlgGenerator::generateAlg(); template void ReductionAlgGenerator::generateAlg(); template void ReductionAlgGenerator::generateAlg(); template void ReductionAlgGenerator::generateAlg(); template void ReductionAlgGenerator::generateAlg(); template void ReductionAlgGenerator::generateAlg(); template ReductionAlgGenerator::ReductionAlgGenerator(VectorOfElements v): ve(v), res(asl::AVec(ve.size())), groupRes(ve.size()) { } template std::shared_ptr> generateSumAlg(VectorOfElements v) { return make_shared>(v); } template std::shared_ptr> generateSumAlg(VectorOfElements v); template std::shared_ptr> generateSumAlg(VectorOfElements v); template std::shared_ptr> generateMinAlg(VectorOfElements v) { return make_shared>(v); } template std::shared_ptr> generateMinAlg(VectorOfElements v); template std::shared_ptr> generateMinAlg(VectorOfElements v); template std::shared_ptr> generateMaxAlg(VectorOfElements v) { return make_shared>(v); } template std::shared_ptr> generateMaxAlg(VectorOfElements v); template std::shared_ptr> generateMaxAlg(VectorOfElements v); template std::shared_ptr> generateProductAlg(VectorOfElements v) { return make_shared>(v); } template std::shared_ptr> generateProductAlg(VectorOfElements v); template std::shared_ptr> generateProductAlg(VectorOfElements v); } // namespace acl