From fa5f848c23042da4d71c12994b03b46818bf5b70 Mon Sep 17 00:00:00 2001 From: "Andrew M. B. Boktor" Date: Wed, 16 May 2012 15:06:13 -0800 Subject: Re-wrote the intermediate structure for the output of cuobjdump using C++ classes Some changes to remove warnings [git-p4: depot-paths = "//depot/gpgpu_sim_research/fermi/distribution/": change = 12609] --- cuobjdump_to_ptxplus/cuobjdumpInst.cc | 4 +- cuobjdump_to_ptxplus/cuobjdump_to_ptxplus.cc | 4 + libcuda/cuda_runtime_api.cc | 191 ++++++++++++++++++--------- src/cuda-sim/ptx_loader.cc | 10 +- src/cuda-sim/ptx_loader.h | 3 +- 5 files changed, 141 insertions(+), 71 deletions(-) diff --git a/cuobjdump_to_ptxplus/cuobjdumpInst.cc b/cuobjdump_to_ptxplus/cuobjdumpInst.cc index c3641fe..e10acef 100644 --- a/cuobjdump_to_ptxplus/cuobjdumpInst.cc +++ b/cuobjdump_to_ptxplus/cuobjdumpInst.cc @@ -29,11 +29,13 @@ #include #include #include +#include // Local includes #include "cuobjdumpInst.h" extern void output(const char * text); +extern void output(const std::string text); //Constructor cuobjdumpInst::cuobjdumpInst() @@ -737,7 +739,7 @@ void cuobjdumpInst::printCuobjdumpOperands() } } -char * int_default_mod () { return ".u32" ;} +std::string int_default_mod () { return ".u32" ;} std::string breaktarget; diff --git a/cuobjdump_to_ptxplus/cuobjdump_to_ptxplus.cc b/cuobjdump_to_ptxplus/cuobjdump_to_ptxplus.cc index 1d1d6b3..570ebf5 100644 --- a/cuobjdump_to_ptxplus/cuobjdump_to_ptxplus.cc +++ b/cuobjdump_to_ptxplus/cuobjdump_to_ptxplus.cc @@ -57,6 +57,10 @@ void output(const char * text) fprintf(ptxplus_out, text); } +void output(const std::string text) { + output(text.c_str()); +} + std::string fileToString(const char * fileName) { ifstream fileStream(fileName, ios::in); string text, line; diff --git a/libcuda/cuda_runtime_api.cc b/libcuda/cuda_runtime_api.cc index 1f95af6..c952ee5 100644 --- a/libcuda/cuda_runtime_api.cc +++ b/libcuda/cuda_runtime_api.cc @@ -107,6 +107,7 @@ #include #include #include +#include #ifdef OPENGL_SUPPORT #define GL_GLEXT_PROTOTYPES #ifdef __APPLE__ @@ -1109,48 +1110,114 @@ enum cuobjdumpSectionType { ELFSECTION }; -typedef struct cuobjdumpSectionRec { - enum cuobjdumpSectionType type; - char* arch; - char* identifier; - char* ptxfilename; - char* elffilename; - char* sassfilename; -}cuobjdumpSection; +class cuobjdumpSection { +public: + //Constructor + cuobjdumpSection() { + arch = 0; + identifier = ""; + } + virtual ~cuobjdumpSection() {} + unsigned getArch() {return arch;} + void setArch(unsigned a) {arch = a;} + std::string getIdentifier() {return identifier;} + void setIdentifier(std::string i) {identifier = i;} + virtual void print(){std::cout << "cuobjdump Section: unknown type" << std::endl;} +private: + unsigned arch; + std::string identifier; +}; + +class cuobjdumpELFSection : public cuobjdumpSection +{ +public: + cuobjdumpELFSection() {} + virtual ~cuobjdumpELFSection() { + elffilename = ""; + sassfilename = ""; + } + std::string getELFfilename() {return elffilename;} + void setELFfilename(std::string f) {elffilename = f;} + std::string getSASSfilename() {return sassfilename;} + void setSASSfilename(std::string f) {sassfilename = f;} + virtual void print() { + std::cout << "ELF Section:" << std::endl; + std::cout << "arch: sm_" << getArch() << std::endl; + std::cout << "identifier: " << getIdentifier() << std::endl; + std::cout << "elf filename: " << getELFfilename() << std::endl; + std::cout << "sass filename: " << getSASSfilename() << std::endl; + std::cout << std::endl; + } +private: + std::string elffilename; + std::string sassfilename; +}; +class cuobjdumpPTXSection : public cuobjdumpSection +{ +public: + cuobjdumpPTXSection(){ + ptxfilename = ""; + } + std::string getPTXfilename() {return ptxfilename;} + void setPTXfilename(std::string f) {ptxfilename = f;} + virtual void print() { + std::cout << "ELF Section:" << std::endl; + std::cout << "arch: sm_" << getArch() << std::endl; + std::cout << "identifier: " << getIdentifier() << std::endl; + std::cout << "ptx filename: " << getPTXfilename() << std::endl; + std::cout << std::endl; + } +private: + std::string ptxfilename; +}; -std::list cuobjdumpSectionList; +std::list cuobjdumpSectionList; // sectiontype: 0 for ptx, 1 for elf void addCuobjdumpSection(int sectiontype){ - cuobjdumpSection x; - x.type = sectiontype? ELFSECTION: PTXSECTION; - cuobjdumpSectionList.push_front(x); - printf("## Adding new section %s\n", x.type==PTXSECTION?"PTX":"ELF"); + if (sectiontype) + cuobjdumpSectionList.push_front(new cuobjdumpELFSection()); + else + cuobjdumpSectionList.push_front(new cuobjdumpPTXSection()); + printf("## Adding new section %s\n", sectiontype==PTXSECTION?"PTX":"ELF"); } void setCuobjdumparch(const char* arch){ + unsigned archnum; + sscanf(arch, "sm_%u", &archnum); + assert (archnum && "cannot have sm_0"); printf("Adding arch: %s\n", arch); - cuobjdumpSectionList.front().arch = strdup(arch); + cuobjdumpSectionList.front()->setArch(archnum); } void setCuobjdumpidentifier(const char* identifier){ printf("Adding identifier: %s\n", identifier); - cuobjdumpSectionList.front().identifier = strdup(identifier); + cuobjdumpSectionList.front()->setIdentifier(identifier); } void setCuobjdumpptxfilename(const char* filename){ printf("Adding ptx filename: %s\n", filename); - cuobjdumpSectionList.front().ptxfilename = strdup(filename); + cuobjdumpSection* x = cuobjdumpSectionList.front(); + if (dynamic_cast(x) == NULL){ + assert (0 && "You shouldn't be trying to add a ptxfilename to an elf section"); + } + (dynamic_cast(x))->setPTXfilename(filename); } void setCuobjdumpelffilename(const char* filename){ - cuobjdumpSectionList.front().elffilename = strdup(filename); + if (dynamic_cast(cuobjdumpSectionList.front()) == NULL){ + assert (0 && "You shouldn't be trying to add a elffilename to an ptx section"); + } + (dynamic_cast(cuobjdumpSectionList.front()))->setELFfilename(filename); } void setCuobjdumpsassfilename(const char* filename){ - cuobjdumpSectionList.front().sassfilename = strdup(filename); + if (dynamic_cast(cuobjdumpSectionList.front()) == NULL){ + assert (0 && "You shouldn't be trying to add a sassfilename to an ptx section"); + } + (dynamic_cast(cuobjdumpSectionList.front()))->setSASSfilename(filename); } extern "C" int cuobjdump_parse(); extern "C" FILE *cuobjdump_in; @@ -1183,11 +1250,11 @@ void extract_code_using_cuobjdump(){ } //! Read file into char* -char* readfile (const char* filename){ - assert (filename != NULL); - FILE* fp = fopen(filename,"r"); +char* readfile (const std::string filename){ + assert (filename != ""); + FILE* fp = fopen(filename.c_str(),"r"); if (!fp) { - printf("ERROR: Could not open file %s for reading\n", filename); + std::cout << "ERROR: Could not open file %s for reading\n" << filename << std::endl; assert (0); } //! finding size of the file @@ -1206,7 +1273,7 @@ char* readfile (const char* filename){ //remove unecessary sm versions from the section list -std::list pruneSectionList(std::list cuobjdumpSectionList, CUctx_st *context) { +std::list pruneSectionList(std::list cuobjdumpSectionList, CUctx_st *context) { unsigned forced_max_capability = context->get_device()->get_gpgpu()->get_config().get_forced_max_capability(); if (context->get_device()->get_gpgpu()->get_config().convert_to_ptxplus()){ if ( (forced_max_capability == 0) || @@ -1216,25 +1283,27 @@ std::list pruneSectionList(std::list cuobjdu } } - std::list prunedList; + std::list prunedList; std::map cuobjdumpSectionMap; - for ( std::list::iterator iter = cuobjdumpSectionList.begin(); + for ( std::list::iterator iter = cuobjdumpSectionList.begin(); iter != cuobjdumpSectionList.end(); iter++){ - unsigned capability = 0; - sscanf(iter->arch,"sm_%u", &capability); - if(capability <= forced_max_capability || forced_max_capability==0){ - if(cuobjdumpSectionMap[iter->identifier] < capability) cuobjdumpSectionMap[iter->identifier] = capability; + unsigned capability = (*iter)->getArch(); + if(dynamic_cast(*iter) != NULL && + (capability <= forced_max_capability || + forced_max_capability==0)) { + if(cuobjdumpSectionMap[(*iter)->getIdentifier()] < capability) cuobjdumpSectionMap[(*iter)->getIdentifier()] = capability; } } - for ( std::list::iterator iter = cuobjdumpSectionList.begin(); + for ( std::list::iterator iter = cuobjdumpSectionList.begin(); iter != cuobjdumpSectionList.end(); iter++){ - unsigned capability = 0; - sscanf(iter->arch,"sm_%u", &capability); - if(capability == cuobjdumpSectionMap[iter->identifier]){ + unsigned capability = (*iter)->getArch(); + if(capability == cuobjdumpSectionMap[(*iter)->getIdentifier()]){ prunedList.push_back(*iter); + } else { + delete *iter; } } return prunedList; @@ -1246,39 +1315,32 @@ std::list pruneSectionList(std::list cuobjdu * Within the section list, find the ELF section corresponding to a given * sm version and identifier */ -cuobjdumpSection* findelfsection(std::list sectionlist, const char* identifier){ +cuobjdumpELFSection* findelfsection(std::list sectionlist, const std::string identifier){ - std::list::iterator iter; + std::list::iterator iter; for ( iter = sectionlist.begin(); iter != sectionlist.end(); iter++ ){ - if(strcmp(identifier, iter->identifier)==0 && - iter->type == ELFSECTION) - return &(*iter); + cuobjdumpELFSection* elfsection; + if((elfsection=dynamic_cast(*iter)) != NULL){ + if(elfsection->getIdentifier() == identifier) + return elfsection; + } } assert(0 && "Could not find the required ELF section"); return NULL; } -//Function that helps debugging that's happening -void printSectionList(std::list sl) { - std::list::iterator iter; +//Function that helps debugging +void printSectionList(std::list sl) { + std::list::iterator iter; for ( iter = sl.begin(); iter != sl.end(); iter++ ){ - if(iter->type == ELFSECTION){ - printf("ELF Section:\n" - "identifier: %s, %s\n" - "elf filename: %s\n" - "sass filename: %s\n\n", iter->identifier, iter->arch, iter->elffilename, iter->sassfilename); - } else { - printf("PTX Section:\n" - "identifier: %s, %s\n" - "ptx filename: %s\n\n", iter->identifier, iter->arch, iter->ptxfilename); - } + (*iter)->print(); } } @@ -1290,31 +1352,30 @@ void useCuobjdump() { cuobjdumpSectionList = pruneSectionList(cuobjdumpSectionList, context); unsigned total_ptx_files = cuobjdumpSectionList.size()/2; - for ( std::list::iterator iter = cuobjdumpSectionList.begin(); - iter != cuobjdumpSectionList.end(); - iter++ + for ( std::list::iterator iter2 = cuobjdumpSectionList.begin(); + iter2 != cuobjdumpSectionList.end(); + iter2++ ){ - unsigned capability = 0; - sscanf(iter->arch,"sm_%u", &capability); - if (iter->type ==PTXSECTION) + if (dynamic_cast(*iter2) != NULL) { + cuobjdumpPTXSection *iter = dynamic_cast(*iter2); symbol_table *symtab; - char *ptxcode = readfile(iter->ptxfilename); + char *ptxcode = readfile(iter->getPTXfilename()); if(context->get_device()->get_gpgpu()->get_config().convert_to_ptxplus() ) { - cuobjdumpSection* elfsection = findelfsection(cuobjdumpSectionList, iter->identifier); + cuobjdumpELFSection* elfsection = findelfsection(cuobjdumpSectionList, iter->getIdentifier()); assert (elfsection!= NULL); char *ptxplus_str = gpgpu_ptx_sim_convert_ptx_and_sass_to_ptxplus( - iter->ptxfilename, - elfsection->elffilename, - elfsection->sassfilename); + iter->getPTXfilename(), + elfsection->getELFfilename(), + elfsection->getSASSfilename()); symtab=gpgpu_ptx_sim_load_ptx_from_string(ptxplus_str,source_num); - printf("Adding %s with cubin handle %u\n", iter->ptxfilename, source_num); + printf("Adding %s with cubin handle %u\n", iter->getPTXfilename().c_str(), source_num); context->add_binary(symtab, source_num); gpgpu_ptxinfo_load_from_string( ptxcode,total_ptx_files-source_num); delete[] ptxplus_str; } else { symtab=gpgpu_ptx_sim_load_ptx_from_string(ptxcode, source_num); - printf("Adding %s with cubin handle %u\n", iter->ptxfilename, source_num); + printf("Adding %s with cubin handle %u\n", iter->getPTXfilename().c_str(), source_num); context->add_binary(symtab,source_num); gpgpu_ptxinfo_load_from_string( ptxcode, total_ptx_files-source_num); } @@ -1739,7 +1800,6 @@ extern "C" int ptx_parse(); extern "C" int ptx__scan_string(const char*); extern "C" FILE *ptx_in; -extern "C" const char *g_ptxinfo_filename; extern "C" int ptxinfo_parse(); extern "C" int ptxinfo_debug; extern "C" FILE *ptxinfo_in; @@ -1850,4 +1910,3 @@ kernel_info_t *gpgpu_cuda_ptx_sim_init_grid( const char *hostFun, return result; } - diff --git a/src/cuda-sim/ptx_loader.cc b/src/cuda-sim/ptx_loader.cc index 690d6ee..4119377 100644 --- a/src/cuda-sim/ptx_loader.cc +++ b/src/cuda-sim/ptx_loader.cc @@ -46,7 +46,7 @@ bool g_override_embedded_ptx = false; extern "C" int ptx_parse(); extern "C" int ptx__scan_string(const char*); -extern "C" const char *g_ptxinfo_filename = NULL; +const char *g_ptxinfo_filename; extern "C" int ptxinfo_parse(); extern "C" int ptxinfo_debug; extern "C" FILE *ptxinfo_in; @@ -97,7 +97,7 @@ void print_ptx_file( const char *p, unsigned source_num, const char *filename ) fflush(stdout); } -char* gpgpu_ptx_sim_convert_ptx_and_sass_to_ptxplus(const char *ptxfilename, const char *elffilename, const char *sassfilename) +char* gpgpu_ptx_sim_convert_ptx_and_sass_to_ptxplus(const std::string ptxfilename, const std::string elffilename, const std::string sassfilename) { printf("GPGPU-Sim PTX: converting EMBEDDED .ptx file to ptxplus \n"); @@ -110,7 +110,11 @@ char* gpgpu_ptx_sim_convert_ptx_and_sass_to_ptxplus(const char *ptxfilename, con // Run cuobjdump_to_ptxplus char commandline[1024]; int result; - snprintf(commandline, 1024, "$GPGPUSIM_ROOT/cuobjdump_to_ptxplus/cuobjdump_to_ptxplus %s %s %s %s", ptxfilename, sassfilename, elffilename, fname_ptxplus); + snprintf(commandline, 1024, "$GPGPUSIM_ROOT/cuobjdump_to_ptxplus/cuobjdump_to_ptxplus %s %s %s %s", + ptxfilename.c_str(), + sassfilename.c_str(), + elffilename.c_str(), + fname_ptxplus); fflush(stdout); printf("GPGPU-Sim PTX: calling cuobjdump_to_ptxplus\ncommandline: %s\n", commandline); result = system(commandline); diff --git a/src/cuda-sim/ptx_loader.h b/src/cuda-sim/ptx_loader.h index 21974e6..cb02eda 100644 --- a/src/cuda-sim/ptx_loader.h +++ b/src/cuda-sim/ptx_loader.h @@ -27,12 +27,13 @@ #ifndef PTX_LOADER_H_INCLUDED #define PTX_LOADER_H_INCLUDED +#include extern bool g_override_embedded_ptx; class symbol_table *gpgpu_ptx_sim_load_ptx_from_string( const char *p, unsigned source_num ); void gpgpu_ptxinfo_load_from_string( const char *p_for_info, unsigned source_num ); -char* gpgpu_ptx_sim_convert_ptx_and_sass_to_ptxplus(const char *ptx_str, const char *sass_str, const char *elf_str); +char* gpgpu_ptx_sim_convert_ptx_and_sass_to_ptxplus(const std::string ptx_str, const std::string sass_str, const std::string elf_str); bool keep_intermediate_files(); #endif -- cgit v1.3