bool RSReflectionCpp::writeBC() { FILE *pfin = fopen(mOutputBCFileName.c_str(), "rb"); if (pfin == NULL) { fprintf(stderr, "Error: could not read file %s\n", mOutputBCFileName.c_str()); return false; } unsigned char buf[16]; int read_length; write("static const unsigned char __txt[] = {"); incIndent(); while ((read_length = fread(buf, 1, sizeof(buf), pfin)) > 0) { string s; for(int i = 0; i < read_length; i++) { char buf2[16]; sprintf(buf2, "0x%02x,", buf[i]); s += buf2; } write(s); } decIndent(); write("};"); write(""); return true; }
void petabricks::CodeGenerator::callSpatial(const std::string& methodname, const SimpleRegion& region) { write("{"); incIndent(); comment("MARKER 3"); write("IndexT _tmp_begin[] = {" + region.getIterationLowerBounds() + "};"); write("IndexT _tmp_end[] = {" + region.getIterationUpperBounds() + "};"); write(methodname+"(_tmp_begin, _tmp_end);"); decIndent(); write("}"); }
void petabricks::CodeGenerator::mkIterationTrampTask(const std::string& taskname, const std::string& /*objname*/, const std::string& methodname, const std::string& metadataclass, const std::string& metadata, const CoordinateFormula& coord) { std::string taskclass = "petabricks::IterationTrampMethodCallTask<CLASS" ", " + metadataclass + ", " + jalib::XToString(coord.size()) + ", &CLASS::" + methodname + ">"; write("{"); incIndent(); write("IndexT _tmp_coord[] = {" + coord.toString() + "};"); write(taskname+" = new "+taskclass+"(this, " + metadata + ", _tmp_coord);"); decIndent(); write("}"); }
void CDECL TraceManager::trace_end(const char* format, ...) throw() { va_list args; va_start(args, format); char buf[512]; _vsnprintf(buf, sizeof(buf), format, args); decIndent(GetCurrentThreadId()); print((string)"END " +buf); va_end(args); }
void petabricks::CodeGenerator::mkPartialSpatialTask(const std::string& taskname, const std::string& metadataname, const std::string& methodname, const SimpleRegion& region, SpatialCallType spatialCallType, bool shouldGenerateMetadata) { JASSERT(spatialCallType == SpatialCallTypes::WORKSTEALING_PARTIAL); JASSERT(!shouldGenerateMetadata); // apply_ruleX_partial std::string taskclass = "petabricks::SpatialMethodCallTask_partial<" + metadataname + ", " + jalib::XToString(region.dimensions() + region.removedDimensions()) + ", &::" + methodname + ">"; write("{"); incIndent(); comment("MARKER 11"); write("IndexT _tmp_begin[] = {" + region.getIterationLowerBounds() + "};"); write("IndexT _tmp_end[] = {" + region.getIterationUpperBounds() + "};"); write(taskname+" = new "+taskclass+"(_tmp_begin, _tmp_end, metadata);"); decIndent(); write("}"); }
void petabricks::CodeGenerator::mkSpatialTask(const std::string& taskname, const std::string& /*objname*/, const std::string& methodname, const SimpleRegion& region, SpatialCallType spatialCallType) { JASSERT(spatialCallType != SpatialCallTypes::WORKSTEALING_PARTIAL); // apply_ruleX std::string suffix = (spatialCallType == SpatialCallTypes::DISTRIBUTED) ? "_distributed" : ""; std::string taskclass = "petabricks::SpatialMethodCallTask" + suffix + "<CLASS, " + jalib::XToString(region.dimensions() + region.removedDimensions()) + ", &CLASS::" + methodname; if (spatialCallType == SpatialCallTypes::DISTRIBUTED) { taskclass += ", &CLASS::" + methodname + "_getDataHosts"; } taskclass += ">"; write("{"); incIndent(); comment("MARKER 6"); write("IndexT _tmp_begin[] = {" + region.getIterationLowerBounds() + "};"); write("IndexT _tmp_end[] = {" + region.getIterationUpperBounds() + "};"); write(taskname+" = new "+taskclass+"(this,_tmp_begin, _tmp_end);"); decIndent(); write("}"); }
bool RSReflectionCpp::makeHeader(const std::string &baseClass) { startFile(mClassName + ".h"); write(""); write("#include \"ScriptC.h\""); write(""); // Imports //for(unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++) //out() << "import " << Import[i] << ";" << std::endl; //out() << std::endl; if(!baseClass.empty()) { write("class " + mClassName + " : public " + baseClass + " {"); } else { write("class " + mClassName + " {"); } write("private:"); uint32_t slot = 0; incIndent(); for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), E = mRSContext->export_vars_end(); I != E; I++, slot++) { const RSExportVar *ev = *I; RSReflectionTypeData rtd; ev->getType()->convertToRTD(&rtd); if(!ev->isConst()) { write(string(rtd.type->c_name) + " __" + ev->getName() + ";"); } } decIndent(); write("public:"); incIndent(); write(mClassName + "(android::renderscriptCpp::RenderScript *rs," + " const char *cacheDir, size_t cacheDirLength);"); write("virtual ~" + mClassName + "();"); write(""); // Reflect export variable slot = 0; for (RSContext::const_export_var_iterator I = mRSContext->export_vars_begin(), E = mRSContext->export_vars_end(); I != E; I++, slot++) { const RSExportVar *ev = *I; RSReflectionTypeData rtd; ev->getType()->convertToRTD(&rtd); if(!ev->isConst()) { write(string("void set_") + ev->getName() + "(" + rtd.type->c_name + " v) {"); stringstream tmp; tmp << slot; write(string(" setVar(") + tmp.str() + ", v);"); write(string(" __") + ev->getName() + " = v;"); write("}"); } write(string(rtd.type->c_name) + " get_" + ev->getName() + "() const {"); if(ev->isConst()) { const clang::APValue &val = ev->getInit(); bool isBool = !strcmp(rtd.type->c_name, "bool"); write(string(" return ") + genInitValue(val, isBool) + ";"); } else { write(string(" return __") + ev->getName() + ";"); } write("}"); write(""); } // Reflect export for each functions for (RSContext::const_export_foreach_iterator I = mRSContext->export_foreach_begin(), E = mRSContext->export_foreach_end(); I != E; I++) { const RSExportForEach *ef = *I; if (ef->isDummyRoot()) { write("// No forEach_root(...)"); continue; } stringstream tmp; tmp << "void forEach_" << ef->getName() << "("; if(ef->hasIn() && ef->hasOut()) { tmp << "android::sp<const android::renderscriptCpp::Allocation> ain"; tmp << ", android::sp<const android::renderscriptCpp::Allocation> aout"; } else if(ef->hasIn()) { tmp << "android::sp<const android::renderscriptCpp::Allocation> ain"; } else { tmp << "android::sp<const android::renderscriptCpp::Allocation> aout"; } if(ef->getParamPacketType()) { for(RSExportForEach::const_param_iterator i = ef->params_begin(), e = ef->params_end(); i != e; i++) { RSReflectionTypeData rtd; (*i)->getType()->convertToRTD(&rtd); tmp << rtd.type->c_name << " " << (*i)->getName(); } } tmp << ") const;"; write(tmp); } // Reflect export function for (RSContext::const_export_func_iterator I = mRSContext->export_funcs_begin(), E = mRSContext->export_funcs_end(); I != E; I++) { //genExportFunction(C, *I); } decIndent(); write("};"); return true; }
bool RSReflectionCpp::makeImpl(const std::string &baseClass) { startFile(mClassName + ".h"); write(""); write("#include \"" + mClassName + ".h\""); write(""); writeBC(); // Imports //for(unsigned i = 0; i < (sizeof(Import) / sizeof(const char*)); i++) //out() << "import " << Import[i] << ";" << std::endl; //out() << std::endl; write(mClassName + "::" + mClassName + "(android::renderscriptCpp::RenderScript *rs, const char *cacheDir, size_t cacheDirLength) :"); write(" ScriptC(rs, __txt, sizeof(__txt), \"" + mInputFileName + "\", 4, cacheDir, cacheDirLength) {"); incIndent(); //... decIndent(); write("}"); write(""); write(mClassName + "::~" + mClassName + "() {"); write("}"); write(""); // Reflect export for each functions uint32_t slot = 0; for (RSContext::const_export_foreach_iterator I = mRSContext->export_foreach_begin(), E = mRSContext->export_foreach_end(); I != E; I++, slot++) { const RSExportForEach *ef = *I; if (ef->isDummyRoot()) { write("// No forEach_root(...)"); continue; } stringstream tmp; tmp << "void " << mClassName << "::forEach_" << ef->getName() << "("; if(ef->hasIn() && ef->hasOut()) { tmp << "android::sp<const android::renderscriptCpp::Allocation> ain"; tmp << ", android::sp<const android::renderscriptCpp::Allocation> aout"; } else if(ef->hasIn()) { tmp << "android::sp<const android::renderscriptCpp::Allocation> ain"; } else { tmp << "android::sp<const android::renderscriptCpp::Allocation> aout"; } tmp << ") const {"; write(tmp); tmp.str(""); tmp << " forEach(" << slot << ", "; if(ef->hasIn() && ef->hasOut()) { tmp << "ain, aout, NULL, 0);"; } else if(ef->hasIn()) { tmp << "ain, NULL, 0);"; } else { tmp << "aout, NULL, 0);"; } write(tmp); write("}"); write(""); } // Reflect export function slot = 0; for (RSContext::const_export_func_iterator I = mRSContext->export_funcs_begin(), E = mRSContext->export_funcs_end(); I != E; I++) { //genExportFunction(C, *I); } decIndent(); return true; }
void petabricks::CodeGenerator::mkCreateGpuSpatialMethodCallTask( const std::string& transname, const std::string& taskname, const std::string& objname, const std::string& methodname, const SimpleRegion& region, std::vector<RegionNodeGroup>& regionNodesGroups, int nodeID, int gpuCopyOut, RegionList to, bool divisible) { std::string taskclass ; int dim_int = region.totalDimensions(); std::string lastdim = jalib::XToString(dim_int - 1); std::string max = region.maxCoord()[dim_int - 1]->toString(); std::string min = region.minCoord()[dim_int - 1]->toString(); if(!divisible) { taskclass = "petabricks::CreateGpuSpatialMethodCallTask<"+objname + ", " + jalib::XToString(region.totalDimensions()) + ", &" + objname + "::" + methodname + TX_OPENCL_POSTFIX + "_createtasks" + ">"; //beginIf(min+"<"+max); comment("MARKER 6"); write("IndexT _tmp_begin[] = {" + region.getIterationLowerBounds() + "};"); write("IndexT _tmp_end[] = {" + region.getIterationUpperBounds() + "};"); write("RegionNodeGroupMapPtr groups = new RegionNodeGroupMap();"); for(std::vector<RegionNodeGroup>::iterator group = regionNodesGroups.begin(); group != regionNodesGroups.end(); ++group){ write("{"); incIndent(); write("std::set<int> ids;"); for(std::vector<int>::iterator id = group->nodeIDs().begin(); id != group->nodeIDs().end(); ++id){ write("ids.insert("+jalib::XToString(*id)+");"); } write("groups->insert(RegionNodeGroup(\""+group->matrixName()+"\",ids));"); decIndent(); write("}"); } write(taskname+" = new "+taskclass+"(this,_tmp_begin, _tmp_end, "+jalib::XToString(nodeID)+", groups, "+jalib::XToString(gpuCopyOut)+");"); //endIf(); return; } std::string n_div = "cont_" + jalib::XToString(_contCounter++); write(taskname + " = new petabricks::MethodCallTask<"+_curClass+", &"+_curClass+"::"+n_div+">( this );"); // Add divider function CodeGenerator helper = forkhelper(); helper.beginFunc("DynamicTaskPtr", n_div); helper.write("DynamicTaskPtr _fini = new NullDynamicTask();"); // Assign the gpu-cpu division point. helper.write("ElementT gpu_ratio = "+transname+"_gpuratio/8.0;"); std::string div = "div"; RegionPtr proxy = to.front(); helper.write("IndexT totalRow = "+proxy->matrix()->name()+".size("+jalib::XToString(dim_int - 1)+");"); helper.write("IndexT div = ceil(gpu_ratio * totalRow);"); helper.beginIf("div > " + max); helper.write("div = "+max+";"); helper.endIf(); // GPU taskclass = "petabricks::CreateGpuSpatialMethodCallTask<"+objname + ", " + jalib::XToString(dim_int) + ", &" + objname + "::" + methodname + TX_OPENCL_POSTFIX + "_createtasks" + ">"; helper.comment("MARKER 6"); //helper.beginIf(min+" < div"); helper.write("IndexT _gpu_begin[] = {" + region.getIterationLowerBounds() + "};"); helper.write("IndexT _gpu_end[] = {" + region.getIterationMiddleEnd(div) + "};"); helper.write("RegionNodeGroupMapPtr groups = new RegionNodeGroupMap();"); for(std::vector<RegionNodeGroup>::iterator group = regionNodesGroups.begin(); group != regionNodesGroups.end(); ++group){ helper.write("{"); helper.incIndent(); helper.write("std::set<int> ids;"); for(std::vector<int>::iterator id = group->nodeIDs().begin(); id != group->nodeIDs().end(); ++id){ helper.write("ids.insert("+jalib::XToString(*id)+");"); } helper.write("groups->insert(RegionNodeGroup(\""+group->matrixName()+"\",ids));"); helper.decIndent(); helper.write("}"); } helper.write("DynamicTaskPtr gpu_task = new "+taskclass+"(this,_gpu_begin, _gpu_end, "+jalib::XToString(nodeID)+", groups, "+jalib::XToString(gpuCopyOut)+");"); helper.write("gpu_task->enqueue();"); helper.write("_fini->dependsOn(gpu_task);"); //helper.endIf(); // CPU taskclass = "petabricks::SpatialMethodCallTask<CLASS" ", " + jalib::XToString(dim_int) + ", &CLASS::" + methodname + "_workstealing_wrap" //+ ", &CLASS::" + methodname + "_workstealing" + ">"; helper.beginIf("div < " + max); helper.beginIf("div < " + min); helper.write("div = "+min+";"); helper.endIf(); helper.comment("MARKER 6"); helper.write("IndexT _cpu_begin[] = {" + region.getIterationMiddleBegin(div) + "};"); helper.write("IndexT _cpu_end[] = {" + region.getIterationUpperBounds() + "};"); helper.write("DynamicTaskPtr cpu_task = new "+taskclass+"(this,_cpu_begin, _cpu_end);"); helper.write("cpu_task->enqueue();"); helper.write("_fini->dependsOn(cpu_task);"); helper.endIf(); helper.write("return _fini;"); helper.endFunc(); }
void petabricks::CodeGenerator::generateMigrationFunctions(){ CodeGenerator& in = forkhelper(); CodeGenerator& out = forkhelper(); CodeGenerator& size = forkhelper(); CodeGenerator& migrateRegion = forkhelper(); CodeGenerator& getDataHosts = forkhelper(); std::vector<std::string> args; args.push_back("char* _buf"); args.push_back("RemoteHost& _host"); size.beginFunc("size_t", "serialSize"); out.beginFunc("void", "serialize", args); args[0] = "const char* _buf"; in.beginFunc("void", "unserialize", args); size.write("size_t _sz = 0;"); std::vector<std::string> args2; args2.push_back("RemoteHost& sender"); migrateRegion.beginFunc("void", "migrateRegions", args2); std::vector<std::string> args3; args3.push_back("DataHostPidList& list"); getDataHosts.beginFunc("void", "getDataHosts", args3); for(ClassMembers::const_iterator i=_curMembers.begin(); i!=_curMembers.end(); ++i){ if(jalib::StartsWith(i->type, "distributed::")) { out.write(i->name + ".serialize(_buf, _host);"); out.write("_buf += " + i->name + ".serialSize();"); in.write(i->name + ".unserialize(_buf, _host);"); in.write("_buf += " + i->name + ".serialSize();"); size.write("_sz += " + i->name + ".serialSize();"); migrateRegion.comment(i->name + ".updateHandlerChain();"); getDataHosts.write(i->name + ".dataHosts(list);"); }else if(i->type == "IndexT" || i->type == "int" || i->type == "double") { out.write("*reinterpret_cast<"+i->type+"*>(_buf) = "+i->name+";"); in.write(i->name+" = *reinterpret_cast<const "+i->type+"*>(_buf);"); size.write("_sz += sizeof("+i->type+");"); in .write("_buf += sizeof("+i->type+");"); out .write("_buf += sizeof("+i->type+");"); }else if(jalib::StartsWith(i->type, "std::vector<")) { out.write("_serialize_vector(_buf, "+i->name+");"); in.write("_unserialize_vector(_buf, "+i->name+");"); size.write("_sz += _serialSize_vector("+i->name+");"); }else if(i->type == "DynamicTaskPtr") { if(i->initializer != "") { in.write(i->name+" = "+i->initializer+";"); } }else{ JASSERT(false)(i->type).Text("cant yet serialize type"); } } size.write("return _sz;"); in.write("_sender = &_host;"); in.endFunc(); out.endFunc(); size.endFunc(); migrateRegion.endFunc(); getDataHosts.endFunc(); hos() << _curClass << "(const char*, RemoteHost&, bool=false);\n"; os() << _curClass << "::" << _curClass << "(const char* _buf, RemoteHost& _host, bool shouldInit){\n"; incIndent(); write("unserialize(_buf, _host);"); beginIf("shouldInit"); write(_curConstructorBody); endIf(); decIndent(); os() << "\n}\n"; beginFunc(_curClass+"*", "_new_constructor", std::vector<std::string>(1,"const char* _buf, RemoteHost& _host"), true); write("return new "+_curClass+"(_buf, _host);"); endFunc(); beginFunc("RemoteObjectGenerator", "generator"); write("return &RemoteTaskReciever<"+_curClass+">::gen;"); endFunc(); }