2008年7月3日 星期四

simple parallel processing in windows

玩data mining有個好處就是在處理大量資料的時候可以盡情的加速...do as whatever you can do :)

所以現在在coding的時候發現可以加速的地方都會加速..

so 底下提供一些sample, 把你要parallel的指令放入一個list, 然後call c_parallen()就可以達到parallel.

如果用MinGW 記得加上 -mwindows
windows.h記得include
  1. #define UNICODE

  2. #include <windows.h>
  3. #include <iostream>
  4. #include <vector>

  5. using namespace std;

  6. PROCESS_INFORMATION* c_fork(const char * cmd){

  7. STARTUPINFO si;
  8. PROCESS_INFORMATION *pi = (PROCESS_INFORMATION*)malloc(sizeof(PROCESS_INFORMATION) );

  9. ZeroMemory( &si, sizeof(si) );
  10. si.cb = sizeof(si);
  11. ZeroMemory( pi, sizeof(PROCESS_INFORMATION) );

  12. TCHAR Tcmd [4096];
  13. mbstowcs(Tcmd, cmd, (sizeof(TCHAR) * strlen(cmd) ));



  14. // Start the child process.
  15. if( !CreateProcess( NULL, // No module name (use command line)
  16. Tcmd, // Command line
  17. NULL, // Process handle not inheritable
  18. NULL, // Thread handle not inheritable
  19. FALSE, // Set handle inheritance to FALSE
  20. 0, // No creation flags
  21. NULL, // Use parent's environment block
  22. NULL, // Use parent's starting directory
  23. &si, // Pointer to STARTUPINFO structure
  24. pi ) // Pointer to PROCESS_INFORMATION structure
  25. )
  26. {
  27. printf( "CreateProcess failed (%d)\n", GetLastError() );
  28. return NULL;
  29. }

  30. return pi;
  31. }

  32. void c_parallel(const char* cmdlist){

  33. ifstream in(cmdlist, ios::binary);
  34. string line;
  35. vector<PROCESS_INFORMATION*> process;
  36. while( ! in.eof()){

  37. getline(in, line);

  38. if( !line.empty() ){

  39. cout << "do:" << line << endl;
  40. process.push_back( c_fork( line.c_str() )) ;


  41. }

  42. }
  43. //joint
  44. for(int i=0; i < process.size(); i++){
  45. WaitForSingleObject( process[i]->hProcess, INFINITE );
  46. }
  47. //close
  48. for(int i=0; i < process.size(); i++){
  49. CloseHandle( process[i]->hProcess );
  50. CloseHandle( process[i]->hThread );
  51. free(process[i]);
  52. }
  53. }

2008年6月12日 星期四

simple byte-oriented file copy

  1. /**
  2. * simple copy image,
  3. * @param src
  4. * @param dest
  5. */
  6. public void copyImage(File src, File dest){
  7. byte[] buffer = new byte[1024];
  8. FileInputStream r = null;
  9. int r_num=0;
  10. try{
  11. r = new FileInputStream( src);
  12. w = new FileOutputStream( dest);
  13. r_num = r.read(buffer, 0, 1024);
  14. while(r_num != -1){
  15. w.write(buffer, 0, r_num);
  16. r_num = r.read(buffer, 0, 1024);
  17. }
  18. }
  19. catch(IOException e){
  20. e.printStackTrace();
  21. }
  22. finally{
  23. try{
  24. if(r != null)
  25. r.close();
  26. if(w != null)
  27. w.close();
  28. }
  29. catch(IOException ex){
  30. ex.printStackTrace();
  31. }
  32. }
  33. }

可用於任何byte-oriented file, ex: image

2008年5月27日 星期二

簡易的java compression and decompression

目前在做之前學長的Video Annotation tool多個model的整合...常常training出來的model感覺檔案都很大...(因為我只有把它tar起來XD)...有鑑於此,,所以寫了一個簡易的compression/decompression的程式..主要改自Simple String Compression Functions 不過為了符合我的需要,input/output都是一個file,,不過這個blog好像沒辦法上傳檔案..XD所以底下大概看一下吧XD

compression 的infile就是原先的tar起來的file, outfile就是compression放置的地方
decompression的動作就是相反.

  1. /**
  2. * the input is the file that we want to compression
  3. * the output is the compressed file
  4. * @param infile
  5. * @param outfile
  6. * @return
  7. * @throws IOException
  8. */
  9. static public void compression(String infile, String outfile)throws IOException{
  10. try{
  11. byte[] buffer = new byte[1024];
  12. int offset = -1;
  13. fin = new BufferedInputStream( new FileInputStream(infile));
  14. fout = new BufferedOutputStream ( new FileOutputStream(outfile));
  15. zout.putNextEntry(new ZipEntry("0"));
  16. while( (offset = fin.read(buffer)) != -1){
  17. zout.write(buffer, 0, offset);
  18. }
  19. zout.closeEntry();
  20. byte[] compressed = out.toByteArray();
  21. zout.close();
  22. fout.write(compressed);
  23. fout.close();
  24. }
  25. catch(IOException e){
  26. new IOException("simpleCompression.compression exception");
  27. }
  28. finally{
  29. if(fin != null){
  30. fin.close();
  31. }
  32. if(fout != null){
  33. fout.flush();
  34. fout.close();
  35. }
  36. }
  37. }
  38. /**
  39. * the input is the compressed file
  40. * the output is the original file
  41. * @param compressed
  42. * @return
  43. * @throws IOException
  44. */
  45. static public void decompression(String infile, String outfile)throws IOException{
  46. int read=0;
  47. byte[] buffer = new byte[1024];
  48. BufferedWriter fout = null;
  49. ZipInputStream zin = null;
  50. try{
  51. fin = new BufferedInputStream( new FileInputStream(infile));
  52. zin = new ZipInputStream(fin);
  53. ZipEntry entry = zin.getNextEntry();
  54. int offset = -1;
  55. while((offset = zin.read(buffer)) != -1) {
  56. out.write(buffer, 0, offset);
  57. }
  58. fout = new BufferedWriter(new FileWriter(outfile));
  59. fout.write(out.toString());
  60. }
  61. catch(IOException e){
  62. new IOException("simpleCompression.decompression exception");
  63. }
  64. finally{
  65. if(fin != null)
  66. fin.close();
  67. if(fout != null){
  68. fout.flush();
  69. fout.close();
  70. }
  71. if(out != null)
  72. out.close();
  73. if(zin != null)
  74. zin.close();
  75. }
  76. }

2008年3月1日 星期六

split edit panel in eclipse

以前用慣了vim的一個功能: split filename

就會把目前editing panel split into two parts..

這個功能蠻適合在編寫code或是找bug的時候用...

然而,這個功能要怎樣在eclipse上實現呢?

其實很簡單...從 window->new Editor ,

之後會產生一個tab,,,把該tab用拖曳的方式拉到上下左右隨便一個就會產生split的效果了

2008年2月17日 星期日

build jbuilder project using eclipse

最近要修改一些學長的code,
就發現了jbuilder一些很不合理的地方...
在jbuilder裡面可以利用一些內建拖曳的功能去實做GUI...
但是jbuilder也會自動幫你import一些屬於他自己內建的library..
ex: import com.borland.jbcl.xxxx.xxx;
也就是說這個project就算給你source code,
除非你用jbuilder這套軟體去修改...否則你也沒辦法rebuild..
(這跟偉大的邪惡帝國有什麼區別....)
不過...還好可以從jbuilder裡面安裝的lib找到想要的.jar
再把想要的.jar加入到classpath底下即可.
以上面的例子來說,可以從安裝jbuilder目錄底下lib/jbcl.jar
然後把該 jar file 的path加入classpath底下,,rebuild,,then 收工!

2008年2月15日 星期五

這是第一篇

這是第一篇文章,,不知道自己會持續寫多久

anyway,就當一個記錄自己在求學過程的一些經驗以及一些人生觀 :)