00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00025
00026 #include <stdlib.h>
00027 #include <vector>
00028 #include <iostream>
00029 using std::vector;
00030 using std::iostream;
00031 #include <time.h>
00032
00033
00034 #ifdef _WIN32
00035 #include <windows.h>
00036
00037 #define SIFTGPU_DLL
00038 #ifdef _DEBUG
00039 #pragma comment(lib, "../../lib/siftgpu_d.lib")
00040 #else
00041 #pragma comment(lib, "../../lib/siftgpu.lib")
00042 #endif
00043 #define thread_t HANDLE
00044 #else
00045 #include <stdio.h>
00046 #include <pthread.h>
00047 #define thread_t pthread_t
00048 pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER;
00049 #endif
00050
00051
00052 #include "../SiftGPU/SiftGPU.h"
00053
00054 class ScopedMutex
00055 {
00056 #ifdef _WIN32
00057 private:
00058 HANDLE hmutex;
00059 public:
00060 ScopedMutex(const char* name) {
00061 hmutex = CreateMutex(NULL, FALSE, name);
00062 WaitForSingleObject(hmutex, INFINITE);
00063 }
00064 ~ScopedMutex()
00065 {
00066 ReleaseMutex(hmutex);
00067 CloseHandle(hmutex);
00068 }
00069 #else
00070 public:
00071 ScopedMutex(const char* name) {
00072 pthread_mutex_lock(&global_mutex);
00073 }
00074 ~ScopedMutex()
00075 {
00076 pthread_mutex_unlock(&global_mutex);
00077 }
00078 #endif
00079 };
00080
00081
00082
00083 class MultiThreadSIFT
00084 {
00085 protected:
00086 SiftGPU* _sift;
00087 const void * _thread_param;
00088 int _device_id;
00089 private:
00090 void Initialize(int device_id)
00091 {
00092 ScopedMutex mutex("siftgpu_initialize");
00093 printf("#%d: Initialize MultiThreadSIFT...", device_id);
00094 InitializeSIFT();
00095 printf("done\n");
00096
00097
00098
00099
00100 }
00101 public:
00102 MultiThreadSIFT(int device_id = 0, const void* thread_param = NULL)
00103 {
00104 _thread_param = thread_param;
00105 _device_id = device_id;
00106 }
00107 virtual ~MultiThreadSIFT()
00108 {
00109 if(_sift) delete _sift;
00110 }
00111
00112 thread_t RunThread()
00113 {
00114 #ifdef _WIN32
00115 return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RunMultiThreadSIFT, this, 0, 0);
00116 #else
00117 thread_t threadid;
00118 pthread_create(&threadid, NULL, RunMultiThreadSIFT, this);
00119 return threadid;
00120 #endif
00121 }
00122 #ifdef _WIN32
00123 static DWORD
00124 #else
00125 static void*
00126 #endif
00127 RunMultiThreadSIFT(void* mts)
00128 {
00129 MultiThreadSIFT* mtsift = (MultiThreadSIFT*) mts;
00130 mtsift->Initialize(mtsift->_device_id);
00131 mtsift->RunTask();
00132 return 0;
00133 }
00134 public:
00135
00136 virtual void RunTask()
00137 {
00138
00139 }
00140
00141 virtual void InitializeSIFT()
00142 {
00144 char device[2] = {'0' + _device_id, '\0'};
00145 char * argv[] = {"-fo", "-1", "-v", "0", "-cuda", device};
00146
00147 int argc = sizeof(argv)/sizeof(char*);
00148
00150 _sift = new SiftGPU;
00151 _sift->ParseParam(argc, argv);
00152
00153
00154 if(_sift->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED) exit(0);
00155 }
00156 };
00157
00158
00159
00160
00161
00162 class MultiThreadDemo: public MultiThreadSIFT
00163 {
00164 public:
00165 MultiThreadDemo(int deviceid, const char* filename): MultiThreadSIFT(deviceid, filename)
00166 {
00167 }
00168 virtual void RunTask()
00169 {
00170 printf("#%d, running task\n", _device_id);
00171 time_t t1, t2;
00172 t1 = time(NULL);
00173 for(int i = 0; i < 100; ++i) _sift->RunSIFT();
00174 t2 = time(NULL);
00175 printf("#%d: %dhz\n", _device_id, int(100/(t2-t1)));
00176 }
00177 virtual void InitializeSIFT()
00178 {
00179 MultiThreadSIFT::InitializeSIFT();
00180 const char* filename = (const char*) _thread_param;
00181 _sift->RunSIFT(filename);
00182
00184
00185
00186
00187
00188
00190 }
00191 };
00192
00193
00194
00195 class MultiProcessDemo: public MultiThreadSIFT
00196 {
00197 public:
00198 MultiProcessDemo(int deviceid, const char* filename): MultiThreadSIFT(deviceid, filename)
00199 {
00200 }
00201 virtual void RunTask()
00202 {
00203 char* filename = (char*) _thread_param;
00204 time_t t1, t2;
00205 t1 = time(NULL);
00206 for(int i = 0; i < 100; ++i) _sift->RunSIFT(filename);
00207 t2 = time(NULL);
00208 printf("Speed on device %d : %dhz\n", _device_id, int(100/(t2-t1)));
00209 }
00210 virtual void InitializeSIFT()
00211 {
00212
00213
00214
00215
00216 char device[2] = {'0' + _device_id, '\0'};
00217 char * argv[] = {"-fo", "-1", "-v", "0", "-cuda", device};
00218 int argc = sizeof(argv)/sizeof(char*);
00219
00221
00222 _sift = CreateRemoteSiftGPU(7777 + _device_id, NULL);
00223 _sift->ParseParam(argc, argv);
00224
00225
00226 if(_sift->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED) exit(0);
00227 }
00228 };
00229
00230
00231 int main()
00232 {
00233
00234 MultiThreadDemo thread1(0, "../data/640-1.jpg");
00235 MultiThreadDemo thread2(1, "../data/800-1.jpg");
00236
00237
00238
00239
00240
00241
00242 printf("Starting two threads...\n");
00243 thread_t t1 = thread1.RunThread();
00244 thread_t t2 = thread2.RunThread();
00245
00246 #ifdef _WIN32
00247 HANDLE handles[2] = {t1, t2};
00248 WaitForMultipleObjects(2, handles, TRUE, INFINITE);
00250 CloseHandle(t1);
00251 CloseHandle(t2);
00252 #else
00253 pthread_join(t1, NULL);
00254 pthread_join(t2, NULL);
00255 #endif
00256 return 1;
00257 }
00258