optimierung



  • hi,

    geht das: man schreibt mehrere versionen einer funktion / klasse, die jeweils auf verschiedene aspekte optimiert sind, und aufgrund von profilingdaten o.ä. sucht sich der compiler dann die richtige aus?



  • pr0fler schrieb:

    hi,
    geht das: man schreibt mehrere versionen einer funktion / klasse, die jeweils auf verschiedene aspekte optimiert sind, und aufgrund von profilingdaten o.ä. sucht sich der compiler dann die richtige aus?

    das geht. ich stelle meine dateien langsam alle um. ich biete alternative implemetierungen und messe auf dem zielsystem für jede kleinigkeit aus, welche alternative schneller ist. das framework dafür besteht im wesentlichen aus folgendem code:

    getBit32.hpp

    #ifndef BITWISE_GETBIT32_HPP
    #define BITWISE_GETBIT32_HPP
    
    #include "../types.hpp"
    #include "getBit32.conf"
    
    #if METHOD_bitwise_getBit32==0
    inline bool getBit(u32 x,int p){
    	return ((x>>p)&1)!=0;
    }
    #elif METHOD_bitwise_getBit32==1
    inline bool getBit(u32 x,int p){
    	bool r;
    	asm(
    		"bt   %[p],%[x];"
    		"setc %[r];"
    		:[r]"=a"(r)
    		:[x]"r"(x),[p]"r"(p)
    	);
    	return r;
    }
    #elif METHOD_bitwise_getBit32==2
    inline bool getBit(u32 x,int p){
    	return 0;
    }
    #else
    inline bool getBit(u32 x,int p){
    	throw 0;
    }
    #endif
    
    #endif
    

    getBit32.cpp

    #include "getBit32.hpp"
    
    #ifdef MEASURE_bitwise_getBit32
    
    #include "../Random.hpp"
    
    int measure(){
        u32 result=0;
        RandomGenerator::MWCG rand(0);
        for(int i=0;i<10000;++i){
            u32 x=rand();
            int p=rand()%32;
            result+=getBit(x,p);
        }
        return result-4935;
    }
    
    #include "../measureMain.hpp"
    
    #endif
    

    getBit32.conf

    #define METHOD_bitwise_getBit32 0
    

    measureMain.hpp

    #include "cout.hpp"
    #include "rdtsc.hpp"
    
    int main() {
    	try {
    		u64 minTime=u64(-1);
    		int times=1000;
    		while (times--) {
    			u64 time=-rdtsc();
    			int r=measure();
    			if (r!=0) {
    				cout<<1<<'\n'<<r<<'\n';
    				break;
    			}
    			time+=rdtsc();
    			if (time<minTime) {
    				minTime=time;
    			}
    		}
    		cout<<'0'<<'\n'<<minTime<<'\n';
    	} catch (int) {
    		cout<<2<<'\n';
    	}
    	return 0;
    }
    

    ../optimize/main.cpp

    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include "Windows.h"
    
    using namespace std;
    
    void writeConf(string const& file,string const& name,int method,bool final=false){
    	ofstream out(file.c_str());
    	if(!final){
    		out<<"#define MEASURE_"<<name<<'\n';
    	}
    	out<<"#define METHOD_"<<name<<' '<<method<<'\n';
    }
    
    void process(string const& base){
    	int i;
    	string file=base+".conf";
    	string name=base;
    	replace(name.begin(),name.end(),'/','_');
    	unsigned long long minTime=(unsigned long long)(-1);
    	int bestMethod=0;
    	for(int method=0;method<10;++method){
    		writeConf(file,name,method);
    		string cmd="make SUBFLAGS=\"-W "+file+"\" makeMeasure";
    		int rc=system(cmd.c_str());
    		if(rc==2){
    			//compilerfehler
    			cout<<"1-method:"<<method<<" rc:"<<rc<<endl;
    			continue;
    		}
    		rc=system("make doMeasure");
    		if(rc!=0){
    			//laufzeitfehler
    			cout<<"2-method:"<<method<<" rc:"<<rc<<endl;
    			continue;
    		}
    		ifstream in("measure.out");
    		unsigned long long time;
    		in>>rc;
    		in>>time;
    		if(rc==1){
    			cout<<"3-method:"<<method<<" wrong result:"<<time<<endl;
    			continue;
    		}
    		if(rc==2){
    			cout<<"method:"<<method<<" ende"<<endl;
    			break;
    		}
    		cout<<"method:"<<method<<" time: "<<time<<endl;
    		if(time<minTime){
    			minTime=time;
    			bestMethod=method;
    		}
    	}
    	writeConf(file,name,bestMethod,true);
    }
    
    int main(int argc,char* argv[]) {
    	for(int i=1;i<argc;++i)
    		process(argv[i]);
    	return 0;
    }
    


  • hi,

    das sieht schonmal ziemlich cool aus 👍.


Anmelden zum Antworten