added working ptxc file
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,275 +0,0 @@
|
||||
#ifndef _ANYOPTION_H
|
||||
#define _ANYOPTION_H
|
||||
|
||||
/* soruce: https://github.com/hackorama/AnyOption/ */
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
#define COMMON_OPT 1
|
||||
#define COMMAND_OPT 2
|
||||
#define FILE_OPT 3
|
||||
#define COMMON_FLAG 4
|
||||
#define COMMAND_FLAG 5
|
||||
#define FILE_FLAG 6
|
||||
|
||||
#define COMMAND_OPTION_TYPE 1
|
||||
#define COMMAND_FLAG_TYPE 2
|
||||
#define FILE_OPTION_TYPE 3
|
||||
#define FILE_FLAG_TYPE 4
|
||||
#define UNKNOWN_TYPE 5
|
||||
|
||||
#define DEFAULT_MAXOPTS 10
|
||||
#define MAX_LONG_PREFIX_LENGTH 2
|
||||
|
||||
#define DEFAULT_MAXUSAGE 3
|
||||
#define DEFAULT_MAXHELP 10
|
||||
|
||||
#define TRUE_FLAG "true"
|
||||
|
||||
|
||||
class AnyOption
|
||||
{
|
||||
|
||||
public: /* the public interface */
|
||||
AnyOption();
|
||||
AnyOption(int maxoptions );
|
||||
AnyOption(int maxoptions , int maxcharoptions);
|
||||
~AnyOption();
|
||||
|
||||
/*
|
||||
* following set methods specifies the
|
||||
* special characters and delimiters
|
||||
* if not set traditional defaults will be used
|
||||
*/
|
||||
|
||||
void setCommandPrefixChar( char _prefix ); /* '-' in "-w" */
|
||||
void setCommandLongPrefix( char *_prefix ); /* '--' in "--width" */
|
||||
void setFileCommentChar( char _comment ); /* '#' in shellscripts */
|
||||
void setFileDelimiterChar( char _delimiter );/* ':' in "width : 100" */
|
||||
|
||||
/*
|
||||
* provide the input for the options
|
||||
* like argv[] for commndline and the
|
||||
* option file name to use;
|
||||
*/
|
||||
|
||||
void useCommandArgs( int _argc, char **_argv );
|
||||
void useFiileName( const char *_filename );
|
||||
|
||||
/*
|
||||
* turn off the POSIX style options
|
||||
* this means anything starting with a '-' or "--"
|
||||
* will be considered a valid option
|
||||
* which alo means you cannot add a bunch of
|
||||
* POIX options chars together like "-lr" for "-l -r"
|
||||
*
|
||||
*/
|
||||
|
||||
void noPOSIX();
|
||||
|
||||
/*
|
||||
* prints warning verbose if you set anything wrong
|
||||
*/
|
||||
void setVerbose();
|
||||
|
||||
|
||||
/*
|
||||
* there are two types of options
|
||||
*
|
||||
* Option - has an associated value ( -w 100 )
|
||||
* Flag - no value, just a boolean flag ( -nogui )
|
||||
*
|
||||
* the options can be either a string ( GNU style )
|
||||
* or a character ( traditional POSIX style )
|
||||
* or both ( --width, -w )
|
||||
*
|
||||
* the options can be common to the commandline and
|
||||
* the optionfile, or can belong only to either of
|
||||
* commandline and optionfile
|
||||
*
|
||||
* following set methods, handle all the aboove
|
||||
* cases of options.
|
||||
*/
|
||||
|
||||
/* options comman to command line and option file */
|
||||
void setOption( const char *opt_string );
|
||||
void setOption( char opt_char );
|
||||
void setOption( const char *opt_string , char opt_char );
|
||||
void setFlag( const char *opt_string );
|
||||
void setFlag( char opt_char );
|
||||
void setFlag( const char *opt_string , char opt_char );
|
||||
|
||||
/* options read from commandline only */
|
||||
void setCommandOption( const char *opt_string );
|
||||
void setCommandOption( char opt_char );
|
||||
void setCommandOption( const char *opt_string , char opt_char );
|
||||
void setCommandFlag( const char *opt_string );
|
||||
void setCommandFlag( char opt_char );
|
||||
void setCommandFlag( const char *opt_string , char opt_char );
|
||||
|
||||
/* options read from an option file only */
|
||||
void setFileOption( const char *opt_string );
|
||||
void setFileOption( char opt_char );
|
||||
void setFileOption( const char *opt_string , char opt_char );
|
||||
void setFileFlag( const char *opt_string );
|
||||
void setFileFlag( char opt_char );
|
||||
void setFileFlag( const char *opt_string , char opt_char );
|
||||
|
||||
/*
|
||||
* process the options, registerd using
|
||||
* useCommandArgs() and useFileName();
|
||||
*/
|
||||
void processOptions();
|
||||
void processCommandArgs();
|
||||
void processCommandArgs( int max_args );
|
||||
bool processFile();
|
||||
|
||||
/*
|
||||
* process the specified options
|
||||
*/
|
||||
void processCommandArgs( int _argc, char **_argv );
|
||||
void processCommandArgs( int _argc, char **_argv, int max_args );
|
||||
bool processFile( const char *_filename );
|
||||
|
||||
/*
|
||||
* get the value of the options
|
||||
* will return NULL if no value is set
|
||||
*/
|
||||
char *getValue( const char *_option );
|
||||
bool getFlag( const char *_option );
|
||||
char *getValue( char _optchar );
|
||||
bool getFlag( char _optchar );
|
||||
|
||||
/*
|
||||
* Print Usage
|
||||
*/
|
||||
void printUsage();
|
||||
void printAutoUsage();
|
||||
void addUsage( const char *line );
|
||||
void addUsage( const std::string &line);
|
||||
void printHelp();
|
||||
/* print auto usage printing for unknown options or flag */
|
||||
void autoUsagePrint(bool flag);
|
||||
|
||||
/*
|
||||
* get the argument count and arguments sans the options
|
||||
*/
|
||||
int getArgc();
|
||||
char* getArgv( int index );
|
||||
bool hasOptions();
|
||||
|
||||
private: /* the hidden data structure */
|
||||
int argc; /* commandline arg count */
|
||||
char **argv; /* commndline args */
|
||||
const char* filename; /* the option file */
|
||||
char* appname; /* the application name from argv[0] */
|
||||
|
||||
int *new_argv; /* arguments sans options (index to argv) */
|
||||
int new_argc; /* argument count sans the options */
|
||||
int max_legal_args; /* ignore extra arguments */
|
||||
|
||||
|
||||
/* option strings storage + indexing */
|
||||
int max_options; /* maximum number of options */
|
||||
const char **options; /* storage */
|
||||
int *optiontype; /* type - common, command, file */
|
||||
int *optionindex; /* index into value storage */
|
||||
int option_counter; /* counter for added options */
|
||||
|
||||
/* option chars storage + indexing */
|
||||
int max_char_options; /* maximum number options */
|
||||
char *optionchars; /* storage */
|
||||
int *optchartype; /* type - common, command, file */
|
||||
int *optcharindex; /* index into value storage */
|
||||
int optchar_counter; /* counter for added options */
|
||||
|
||||
/* values */
|
||||
char **values; /* common value storage */
|
||||
int g_value_counter; /* globally updated value index LAME! */
|
||||
|
||||
/* help and usage */
|
||||
const char **usage; /* usage */
|
||||
std::vector<std::string> usage_string;
|
||||
int max_usage_lines; /* max usage lines reseverd */
|
||||
int usage_lines; /* number of usage lines */
|
||||
|
||||
bool command_set; /* if argc/argv were provided */
|
||||
bool file_set; /* if a filename was provided */
|
||||
bool mem_allocated; /* if memory allocated in init() */
|
||||
bool posix_style; /* enables to turn off POSIX style options */
|
||||
bool verbose; /* silent|verbose */
|
||||
bool print_usage; /* usage verbose */
|
||||
bool print_help; /* help verbose */
|
||||
|
||||
char opt_prefix_char; /* '-' in "-w" */
|
||||
char long_opt_prefix[MAX_LONG_PREFIX_LENGTH + 1]; /* '--' in "--width" */
|
||||
char file_delimiter_char; /* ':' in width : 100 */
|
||||
char file_comment_char; /* '#' in "#this is a comment" */
|
||||
char equalsign;
|
||||
char comment;
|
||||
char delimiter;
|
||||
char endofline;
|
||||
char whitespace;
|
||||
char nullterminate;
|
||||
|
||||
bool set; //was static member
|
||||
bool once; //was static member
|
||||
|
||||
bool hasoptions;
|
||||
bool autousage;
|
||||
|
||||
private: /* the hidden utils */
|
||||
void init();
|
||||
void init(int maxopt, int maxcharopt );
|
||||
bool alloc();
|
||||
void cleanup();
|
||||
bool valueStoreOK();
|
||||
|
||||
/* grow storage arrays as required */
|
||||
bool doubleOptStorage();
|
||||
bool doubleCharStorage();
|
||||
bool doubleUsageStorage();
|
||||
|
||||
bool setValue( const char *option , char *value );
|
||||
bool setFlagOn( const char *option );
|
||||
bool setValue( char optchar , char *value);
|
||||
bool setFlagOn( char optchar );
|
||||
|
||||
void addOption( const char* option , int type );
|
||||
void addOption( char optchar , int type );
|
||||
void addOptionError( const char *opt);
|
||||
void addOptionError( char opt);
|
||||
bool findFlag( char* value );
|
||||
void addUsageError( const char *line );
|
||||
bool CommandSet();
|
||||
bool FileSet();
|
||||
bool POSIX();
|
||||
|
||||
char parsePOSIX( char* arg );
|
||||
int parseGNU( char *arg );
|
||||
bool matchChar( char c );
|
||||
int matchOpt( char *opt );
|
||||
|
||||
/* dot file methods */
|
||||
char *readFile();
|
||||
char *readFile( const char* fname );
|
||||
bool consumeFile( char *buffer );
|
||||
void processLine( char *theline, int length );
|
||||
char *chomp( char *str );
|
||||
void valuePairs( char *type, char *value );
|
||||
void justValue( char *value );
|
||||
|
||||
void printVerbose( const char *msg );
|
||||
void printVerbose( char *msg );
|
||||
void printVerbose( char ch );
|
||||
void printVerbose( );
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif /* ! _ANYOPTION_H */
|
||||
228
examples_ptx/ptxcc/ptxc.cpp
Normal file
228
examples_ptx/ptxcc/ptxc.cpp
Normal file
@@ -0,0 +1,228 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include "PTXParser.h"
|
||||
|
||||
static char lRandomChar()
|
||||
{
|
||||
const char charset[] =
|
||||
"0123456789"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz";
|
||||
const size_t max_index = (sizeof(charset) - 1);
|
||||
return charset[ rand() % max_index ];
|
||||
}
|
||||
|
||||
static std::string lRandomString(const size_t length)
|
||||
{
|
||||
std::string str(length,0);
|
||||
std::generate_n( str.begin(), length, lRandomChar);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
static void lGetAllArgs(int Argc, char *Argv[], int &argc, char *argv[128])
|
||||
{
|
||||
// Copy over the command line arguments (passed in)
|
||||
for (int i = 0; i < Argc; ++i)
|
||||
argv[i] = Argv[i];
|
||||
argc = Argc;
|
||||
}
|
||||
const char *lGetExt (const char *fspec)
|
||||
{
|
||||
char *e = strrchr (fspec, '.');
|
||||
return e;
|
||||
}
|
||||
|
||||
static std::vector<std::string> lSplitString(const std::string &s, char delim)
|
||||
{
|
||||
std::vector<std::string> elems;
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, delim)) {
|
||||
if (!item.empty())
|
||||
elems.push_back(item);
|
||||
}
|
||||
return elems;
|
||||
}
|
||||
|
||||
static void usage(const int ret)
|
||||
{
|
||||
exit(ret);
|
||||
}
|
||||
|
||||
int main(int _argc, char * _argv[])
|
||||
{
|
||||
int argc;
|
||||
char *argv[128];
|
||||
lGetAllArgs(_argc, _argv, argc, argv);
|
||||
|
||||
std::string arch="sm_35";
|
||||
std::string filePTX;
|
||||
std::string fileOBJ;
|
||||
std::string extString = ".ptx";
|
||||
bool keepTemporaries = false;
|
||||
|
||||
std::vector<std::string> nvccArgumentList;
|
||||
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
if (!strcmp(argv[i], "--help"))
|
||||
usage(0);
|
||||
else if (!strncmp(argv[i], "--arch=", 7))
|
||||
arch = std::string(argv[i]+7);
|
||||
else if (!strncmp(argv[i], "--keep-temporaries", 11))
|
||||
keepTemporaries = true;
|
||||
else if (!strcmp(argv[i], "-o"))
|
||||
{
|
||||
if (++i == argc)
|
||||
{
|
||||
fprintf(stderr, "No output file specified after -o option.\n");
|
||||
usage(1);
|
||||
}
|
||||
fileOBJ = std::string(argv[i]);
|
||||
}
|
||||
else if (strncmp(argv[i], "-", 1))
|
||||
{
|
||||
const char * ext = strrchr(argv[i], '.');
|
||||
if (ext == NULL)
|
||||
{
|
||||
fprintf(stderr, " Unknown argument: %s \n", argv[i]);
|
||||
exit(1);
|
||||
}
|
||||
else if (strncmp(ext, extString.c_str(), 4))
|
||||
{
|
||||
fprintf(stderr, " Unkown extension of the input file: %s \n", ext);
|
||||
exit(1);
|
||||
}
|
||||
else if (filePTX.empty())
|
||||
{
|
||||
filePTX = std::string(argv[i]);
|
||||
if (fileOBJ.empty())
|
||||
{
|
||||
char * baseName = argv[i];
|
||||
while (baseName != ext)
|
||||
fileOBJ += std::string(baseName++,1);
|
||||
}
|
||||
fileOBJ += ".o";
|
||||
}
|
||||
}
|
||||
else
|
||||
nvccArgumentList.push_back(argv[i]);
|
||||
}
|
||||
#if 0
|
||||
fprintf(stderr, " fileOBJ= %s\n", fileOBJ.c_str());
|
||||
fprintf(stderr, " arch= %s\n", arch.c_str());
|
||||
fprintf(stderr, " file= %s\n", filePTX.empty() ? "$stdin" : filePTX.c_str());
|
||||
fprintf(stderr, " num_args= %d\n", (int)nvccArgumentList.size());
|
||||
for (int i= 0; i < (int)nvccArgumentList.size(); i++)
|
||||
fprintf(stderr, " arg= %d : %s \n", i, nvccArgumentList[i].c_str());
|
||||
#endif
|
||||
assert(arch == std::string("sm_35"));
|
||||
if (filePTX.empty())
|
||||
{
|
||||
fprintf(stderr, "ptxc fatal : No input file specified; use option --help for more information\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// open a file handle to a particular file:
|
||||
std::ifstream inputPTX(filePTX.c_str());
|
||||
if (!inputPTX)
|
||||
{
|
||||
fprintf(stderr, "ptxc: error: %s: No such file\n", filePTX.c_str());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::string randomBaseName = std::string("/tmp/") + lRandomString(12);
|
||||
fprintf(stderr, " randomBaseName= %s\n", randomBaseName.c_str());
|
||||
|
||||
std::string fileCU= randomBaseName + ".cu";
|
||||
std::ofstream outputCU(fileCU.c_str());
|
||||
assert(outputCU);
|
||||
|
||||
std::istream & input = inputPTX;
|
||||
std::ostream & output = outputCU;
|
||||
std::ostream & error = std::cerr;
|
||||
parser::PTXLexer lexer(&input, &error);
|
||||
parser::PTXParser state(output);
|
||||
|
||||
// parse through the input until there is no more:
|
||||
//
|
||||
|
||||
do {
|
||||
ptx::yyparse(lexer, state);
|
||||
}
|
||||
while (!input.eof());
|
||||
|
||||
inputPTX.close();
|
||||
outputCU.close();
|
||||
|
||||
// process output from nvcc
|
||||
//
|
||||
/* nvcc -dc -arch=$arch -dryrun -argumentlist fileCU */
|
||||
|
||||
std::string fileSH= randomBaseName + ".sh";
|
||||
|
||||
std::string nvccExe("nvcc");
|
||||
std::string nvccCmd;
|
||||
nvccCmd += nvccExe + std::string(" ");
|
||||
nvccCmd += "-dc ";
|
||||
nvccCmd += std::string("-arch=") + arch + std::string(" ");
|
||||
nvccCmd += "-dryrun ";
|
||||
for (int i = 0; i < (int)nvccArgumentList.size(); i++)
|
||||
nvccCmd += nvccArgumentList[i] + std::string(" ");
|
||||
nvccCmd += std::string("-o ") + fileOBJ + std::string(" ");
|
||||
nvccCmd += fileCU + std::string(" ");
|
||||
nvccCmd += std::string("2> ") + fileSH;
|
||||
fprintf(stderr , " nvccCmd= %s\n", nvccCmd.c_str());
|
||||
std::system(nvccCmd.c_str());
|
||||
|
||||
|
||||
std::ifstream inputSH(fileSH.c_str());
|
||||
assert(inputSH);
|
||||
std::vector<std::string> nvccSteps;
|
||||
while (!inputSH.eof())
|
||||
{
|
||||
nvccSteps.push_back(std::string());
|
||||
std::getline(inputSH, nvccSteps.back());
|
||||
}
|
||||
inputSH.close();
|
||||
|
||||
for (int i = 0; i < (int)nvccSteps.size(); i++)
|
||||
{
|
||||
std::string cmd = nvccSteps[i];
|
||||
for (int j = 0; j < (int)cmd.size()-1; j++)
|
||||
if (cmd[j] == '#' && cmd[j+1] == '$')
|
||||
cmd[j] = cmd[j+1] = ' ';
|
||||
std::vector<std::string> splitCmd = lSplitString(cmd, ' ');
|
||||
|
||||
if (!splitCmd.empty())
|
||||
{
|
||||
if (splitCmd[0] == std::string("cicc"))
|
||||
cmd = std::string("cp ") + filePTX + std::string(" ") + splitCmd.back();
|
||||
if (splitCmd[0] == std::string("LIBRARIES="))
|
||||
cmd = "";
|
||||
}
|
||||
nvccSteps[i] = cmd;
|
||||
const int ret = std::system(cmd.c_str());
|
||||
if (ret)
|
||||
{
|
||||
fprintf(stderr, " Something went wrong .. \n");
|
||||
for (int j = 0; j < i; j++)
|
||||
fprintf(stderr, "PASS: %s\n", nvccSteps[j].c_str());
|
||||
fprintf(stderr, "FAIL: %s\n", nvccSteps[i].c_str());
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!keepTemporaries)
|
||||
{
|
||||
/* remove temporaries */
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user