Updates from 四月, 2015 Toggle Comment Threads | 键盘快捷键

  • jinzihao pm3:49 on 2015年4月28日 链接地址 | 回复  

    Input:

    float x[5] = {1, 2, 3, 4, 5};
    cout << (&x[0])[4] << endl;
    

    Output:

    5
    
     
  • jinzihao pm3:41 on 2015年4月28日 链接地址 | 回复  

    #include 
    #include  
    using namespace std;  
    
    class test {
    public:
    	test(){}
    	~test(){}
    	
    	test& operator ()(const std::string& _name,const std::string _dest = "") {
    		name = _name;
    		dest = _dest;
    		return *this;
    	}
    	inline std::string getName(){return name;}
    	inline std::string getDest(){return dest;}
    private:
    	std::string name;
    	std::string dest;
    };
    
    
    int main() {//.addoption()
    	test Ctest;
    	Ctest("1","a")("2","b")("3","c");//.operator ()	
    	std::cout << "name:" << Ctest.getName() << " dest:" << Ctest.getDest() << std::endl;
    }
    
     
  • jinzihao am10:29 on 2015年4月18日 链接地址 | 回复  

    搭建OpenVPN服务器,解决客户端连接到服务器无法访问互联网的问题:
    在/etc/sysctl.conf中,取消#net.ipv4.ip_forward=1的注释(删去#)
    执行:

    iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
    iptables -A INPUT -p UDP --dport 53 -j ACCEPT
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    iptables-save > /root/iptables.conf
    

    其中10.8.0.0/24、UDP和53根据实际情况修改
    在/etc/rc.local中,在exit 0前加入一行

    iptables-restore < /root/iptables.conf
    
     
  • jinzihao am9:50 on 2015年4月13日 链接地址 | 回复  

    #include 
    using namespace std;
    
    int &test1;();
    int &test2;();
    int &test3;();
    
    int main() {
    	int &y1; = test1();
    	int &y2; = test2();
    	int &y3; = test3();
    	cout << y1 << " " << y2 << " " << y3 << endl;
    	for (int i = 0; i < 8; i++) {
    		y1++;
    	}
    	for (int i = 0; i < 16; i++) {
    		y2++;
    	}
    	for (int i = 0; i < 32; i++) {
    		y3++;
    	}
    	cout << y1 << " " << y2 << " " << y3 << endl;
    	return 0;
    }
    
    int &test1() {
    	int x = 42;
    	return x;
    }
    
    int &test2() {
    	int x = 42;
    	return x;
    }
    
    int &test3() {
    	static int x = 42;
    	return x;
    }
    

    运行结果:

    42 42 42
    24 24 74
    
     
  • jinzihao pm10:57 on 2015年4月12日 链接地址 | 回复  

    编译OpenTTD除了安装SDL,还需要安装lzma

    apt-get install lzma lzma-dev liblzma-dev liblzo2-dev
    
     
  • jinzihao pm10:50 on 2015年4月12日 链接地址 | 回复  

    安装SDL:

    apt-get install libsdl-image1.2-dev
    apt-get install libsdl-mixer1.2-dev
    apt-get install libsdl-ttf2.0-dev
    apt-get install libsdl-gfx1.2-dev
    
     
  • jinzihao pm11:17 on 2015年4月11日 链接地址 | 回复  

    编译运行LiquidFun:
    apt-get install cmake
    apt-get install libgl1-mesa-dev
    apt-get install freeglut3-dev
    apt-get install libxi-dev

     
  • jinzihao pm11:12 on 2015年4月7日 链接地址 | 回复  

    针对向量化进行优化的实验:
    在使用g++ -O3编译的条件下,向量化将运行时间降至baseline的38.4%
    在使用icc -O3编译的条件下,向量化将运行时间降至baseline的25.3%

    baseline:

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    double matrix[102][102][102];
    int main(int argc, char **argv) {
    	
    	if (argc != 3) {
    		cout << "Usage: ./1  " << endl; 
    		return 0;
    	}
    	
    	const int dimX = 100; 
    	const int dimY = 100; 
    	const int dimZ = 100; 
    	
    	cout << "Loading data... ";
    	ifstream fin(argv[1]);
    	for (int i = 1; i < dimX + 1; i++) {
    		for (int j = 1; j < dimY + 1; j++) {
    			for (int k = 1; k < dimZ + 1; k++) {
    				char buffer[100];
    				fin >> buffer;
    				matrix[i][j][k] = atof(buffer);
    			}
    		}
    	}
    	cout << "complete." << endl;
    	
    	int times = atoi(argv[2]);
    	
    	timeval t1;
    	gettimeofday (&t1, NULL); 
    	for (int n = 0; n < times; n++) {
    		for (int i = 1; i < dimX + 1; i++) {
    			for (int j = 1; j < dimY + 1; j++) {
    				for (int k = 1; k < dimZ + 1; k++) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    			}
    		}
    	}
    	timeval t2;
    	gettimeofday (&t2, NULL); 
    	
    	cout << "Time elapsed: " << (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec << " microseconds" << endl;
    	return 0;
    }
    

    针对向量化优化的版本:

    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    double matrix[102][102][102];
    int main(int argc, char **argv) {
    	
    	if (argc != 3) {
    		cout << "Usage: ./1  " << endl; 
    		return 0;
    	}
    	
    	const int dimX = 100; 
    	const int dimY = 100; 
    	const int dimZ = 100; 
    	
    	cout << "Loading data... ";
    	ifstream fin(argv[1]);
    	for (int i = 1; i < dimX + 1; i++) {
    		for (int j = 1; j < dimY + 1; j++) {
    			for (int k = 1; k < dimZ + 1; k++) {
    				char buffer[100];
    				fin >> buffer;
    				matrix[i][j][k] = atof(buffer);
    			}
    		}
    	}
    	cout << "complete." << endl;
    	
    	int times = atoi(argv[2]);
    	
    	timeval t1;
    	gettimeofday (&t1, NULL); 
    	for (int n = 0; n < times; n++) {
    		for (int i = 1; i < dimX + 1; i+=2) {
    			for (int j = 1; j < dimY + 1; j+=2) {
    				for (int k = 1; k < dimZ + 1; k+=2) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    				for (int k = 2; k < dimZ + 1; k+=2) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    			}
    			for (int j = 2; j < dimY + 1; j+=2) {
    				for (int k = 1; k < dimZ + 1; k+=2) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    				for (int k = 2; k < dimZ + 1; k+=2) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    			}
    		}
    		for (int i = 2; i < dimX + 1; i+=2) {
    			for (int j = 1; j < dimY + 1; j+=2) {
    				for (int k = 1; k < dimZ + 1; k+=2) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    				for (int k = 2; k < dimZ + 1; k+=2) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    			}
    			for (int j = 2; j < dimY + 1; j+=2) {
    				for (int k = 1; k < dimZ + 1; k+=2) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    				for (int k = 2; k < dimZ + 1; k+=2) {
    					matrix[i][j][k] = (matrix[i][j][k-1] + matrix[i][j][k+1] + matrix[i][j-1][k] + matrix[i][j+1][k] + matrix[i-1][j][k] + matrix[i+1][j][k]) / 6;
    				}
    			}
    		}
    	}
    	timeval t2;
    	gettimeofday (&t2, NULL); 
    	
    	cout << "Time elapsed: " << (t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec << " microseconds" << endl;
    	return 0;
    }
    
     
c
写新的
j
下一篇文章/下一个回复
k
前一篇文章/以前的回复
r
回复
e
编辑
o
显示/隐藏 回复
t
回到顶部
l
go to login
h
show/hide help
shift + esc
取消