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. }