網頁

2017年2月15日 星期三

[OpenCV] LUT access time comparison


比較 OpenCV 做 Look Up Table 的效率.
OpenCV 提供了幾種作 Look Up Table 的動作的方法. 以下對這幾種方法做效率上的比較, 同時加上以 C 的 Pointer 的存取方式.
Index Reference Code ms
C Pointer for( int i = 0; i < scan_size; i ++ )
{
    * pDest = pTable[ * pSrc ];
    pDest ++;
    pSrc ++;
}
4.98
C [] for( i = 0; i < nRows; ++i)
{
    p = I.ptr<uchar>(i);
    for ( j = 0; j < nCols; ++j)
    {
        p[j] = table[p[j]];
    }
}
5.12
iterator MatIterator_<Vec3b> it, end;
for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
{
    (*it)[0] = table[(*it)[0]];
    (*it)[1] = table[(*it)[1]];
    (*it)[2] = table[(*it)[2]];
}
170.45
Random Access Mat_<Vec3b> _I = I;
for( int i = 0; i < I.rows; ++i)
   for( int j = 0; j < I.cols; ++j )
      {
          _I(i,j)[0] = table[_I(i,j)[0]];
          _I(i,j)[1] = table[_I(i,j)[1]];
          _I(i,j)[2] = table[_I(i,j)[2]];
   }
I = _I;
291.56
LUT
    LUT(I, lookUpTable, J);
5.72
Note :
  1. C Pointer 的作法只是做為一個參考, 以 C 程式指令來說, 應該已經是最快的, 這裡主要是用來作為對比.
  2. C [] 所耗費的時間略多, 主要應該是用到索引定址的關係. 因為它要先做計數器累加.
  3. OpenCV 的 LUT() 函式耗費時間較多. 通用型的函式, 一般來說, 都會比特用型的函式, 來得慢一些, 這樣的時間, 是滿不錯的了.
  4. iterator 的存取方式的耗時, 非常的不好, 是 30~40 倍的慢. 顯然這種存取方式, 並不適合用來做 LookUpTable 這種對大量記憶體做簡單運算的動作. 它在存取每一個點的 overhead 太大, 需要做 iterator 前進到下一點的動作, 對每一點的存取也需要做多次的計數器及取址.
  5. Random Access, 用 Random Access 的方法, 來做 Sequential Access, 就好像拿拖把掃地, 這邊也是作為參考.

2017年2月12日 星期日

1.5 ms 解出數獨的簡易程式解法

 

朋友傳了一個數獨的題目給我, 這應該算是一個相當難的題目. 原題如下 :

6           4   5
    8 2          
                 
          6   1  
4   7            
      1       2  
    6       7 8  
        5 4      
      9          
 

很明顯的,超多的空格, 意味著會有一些需要猜測. 所以這一題會很難. 我不想花時間去手動解, 又很好奇, 空格這麼多, 是不是會有多數解 ? 所以, 我們會需要一個程式, 可以跑出所有的解法.

數獨的解法, 曾經是一個流行的題目. 主要的關鍵字, EXACT COVER Problem, DLX, Dancing Link, Algorithm X, 都可以找到許多有用的資料. 和這些解法來比, 這邊提出來的解法, 只能說是簡單,直覺,一個普通人的解法, 有興趣的可以參考.

一個直覺的解法

數獨的解決過程, 可以分成推論和猜測以及判斷三個部分. 以下簡單的說明:

推論:

根據數獨的規則, 我們要對每一格, 所對應到的列, 行, 區塊, 計算它的可以填入數字的集合. 而這個格子的可填入數字, 就是行,列,區塊, 的交集. 譬如說, 第 7 列, 第 4 行, 這一格, 所對應到的 第 7 列, 第 4 行, 第 8 個區塊, 他們的可填入數字, 分別是 第 7 列 { 1, 2, 3, 4, 5, 9}, 第 4 行 { 3, 4, 5, 6, 7, 8 }, 第 8 區塊 {  2, 3, 6, 7, 8 }, 這幾個集合的交集是 { 3 } , 所以 S[7][4] 這一格的答案是 3.

猜測:
6           4   5
    8 2          
                 
          6   1  
4   7            
      1       2  
    6 3     7 8  
        5 4      
      9          

 

這一個階段, 已經沒有前述可以推論得知的答案, 考量 S[1][4], 第 1 , 第 1 列的可行解是 { 1, 2, 3, 7, 8, 9 }, 第 4 行的可行解是 { 4, 5, 6, 7, 8 }, 第 2 區塊的可行解是 { 1, 3, 4, 5, 6, 7, 8, 9}, 所以 S[1][4] 這一格的可行解是 { 7, 8 }, 我們暫時沒有線索可以判斷應該是 7 還是 8. 所以這時就需要去猜測. 猜測的方式是把題目分成兩個子題. 既然是猜測, 就有可能是錯誤, 所以會需要判斷.

判斷:
6     7     4 3 5
    8 2       7  
      4          
      8   6   1  
4   7 5       6  
      1       2  
    6 3     7 8  
      6 5 4   9  
      9          

 

考量 S[3][8] 這一格, 第 3 列的可行解是 { 1, 2, 3, 5, 6, 7, 8, 9}, 第 8 行的 可行解是 { 4, 5}, 第 3 區塊的可行解是 { 1, 2, 6, 8, 9}, S[3][8] 這一格的可行解是沒有. 原因是我們在前面的猜測錯誤, 這一個子題不存在解決方案.

一般的數獨的解決過程, 就是重複推論, 猜測, 判斷的過程. 也是最直覺的解法.

資料表示 :

從以上的這三個部份, 我們可以發現, 解題的過程, 就是在列, 行, 區塊的可行解的操作運算, 為了避免重複無意義的計算, 我們要把這些資訊記錄下來. Row/Column/Block 分別用來表示列, 行, 區塊的可行解. 由於可行解是 1-9 的數字集合, 並且只有存在及不存在兩種狀況, 因此用一個 unsigned short.  另外每一格的解決進度, 也會記錄下來.  接下來說明如何用這個資料結構來進行操作.

typedef struct tagSUDOKU_BOARD
{
    char Pattern[CELL_COUNT + 1];                       // For displaying
    unsigned short CellSet[CELL_COUNT];              // For Cell status tracking

    unsigned short RowSet[DIGIT_COUNT];             // Row possible set
    unsigned short ColumnSet[DIGIT_COUNT];        // Column possible set
    unsigned short BlockSet[DIGIT_COUNT];           // Block possible set
}    SUDOKU_BOARD, *LPSUDOKU_BOARD;

啟始動作 :
  1. 預期的輸入, 是一個 81 個字元的字串, 所以要先把字串轉換成前述的資料表示. 檢查相對應字串的位置, 1~9 的數字代表這個位置已指定, 否則即未指定, 是要解決的部分.
  2. 對每一列, 每一行, 每一區塊, 計算它的可行解.
推論動作 :
  1. 對每一個格子, 計算它的可行解. 計算方式是對前述列, 行, 區塊, 作位元 AND 運算.
  2. 運算結果計算它的位元為 1 的數量. 程式中以查表得出. 數量為 1 , 即為得出的答案.
  3. 更新格子的字元表示, 可行解設定為 0. 代表已解.
  4. 更新此格子所對應到的列, 行, 區塊可行解.
猜測動作 :
  1. 對無法找出推論的子題, 進行猜測.
  2. 找出具有最小可行解的格子.
  3. 分別指定此格為可行解的其中之一
  4. 更新此格子所對應到的列, 行, 區塊可行解.
  5. 將此區塊加入待解問題列.
判斷動作 :
  1. 對每一個格子, 計算它的可行解. 計算方式是對前述列, 行, 區塊, 作位元 AND 運算.
  2. 如果任一未指定格子, 它的可行解為空, 那麼這一個子題就不存在解法.

效率 :

  1. 數獨的解出速度, 會大幅度的和題目相關, 這一個題目頗為複雜, 解出的速度應該很有代表性. 一般的題目所需消耗時間不會高於這個時間.
  2. 在 NoteBook 上實測, 可以達到 1.54 ms 解完的速度.

下載程式:

    https://github.com/lxnick/SudokuSolver

2017年1月23日 星期一

[VC] Multi-Threading data transfer 框架

 

Multi-Threading 現在已經是一種普遍應用的技術, 但是對初次接觸的人來說, 並不是那麼容易上手.手上有一個測試程式, 用這個例子做個說明, 並且提供一個完整框架.

這個例子是透過 USB, 取得一個 Frame (畫面), 做一些計算處理, 同時顯示到螢幕上. 所謂的同時, 是說計算處理並不和顯示有直接相關. 計算主要是計算它的解像力, Lens Shading, 這些計算比較耗時, 結果並不需要更新到畫面. 顯示的目的只是提供操作人員可以看到畫面.

由這樣的應用目的, 可以導出如下的需求:

  • 盡力收取影像, 提高計算部分的 frame rate –> 效率考量.
  • 計算或顯示如果運算來不及, 可以放棄目前收進來的  frame.
  • frame 必須完整. 計算完成前, 影像資料不能被更動.

為了因應這三個不同的工作內容( 取得影像, 計算, 顯示), 程式部分區分成 3 個 thread.

  • Master Thread, 身兼管理其他 thread 的任務. 同步機制, 分配記憶體, 取得影像, 發送影像. 以及結束後收拾殘局.
  • Computing Thread. 默默地計算影像資料.
  • Display Thread. 更新影像到螢幕上.

Computing Thread  及 Display Thread 在這個例子中是一樣的. 程式的流程簡單說明如下

  • CMasterThread ( 封裝 MasterThread . 及相關變數)
  • CMasterThread::Create() 嘗試建立 CMasterThread 物件, 並啟動 CMasterThread 的 ThreadFunction
  • CMasterThread::ThreadFunction() 為 ComputingThread, DisplayThread 配置同步物件, CriticalSection, Event, Image Buffer.
  • CMasterThread::ThreadFunction() 建立 Shutdown 作為通知所有 Thread 系統即將關閉的同步機制.
  • CMasterThread::ThreadFunction() 啟動 Display Thread
  • CMasterThread::ThreadFunction() 啟動 Computing Thread
  • -- 此時 Display Thread, Computing Thread 雖已啟動, 但會等不到資料 ready 的 event,  而進入等待狀態
  • CMasterThread::ThreadFunction() 配置接收記憶體
  • -- 此時 CMasterThread::ThreadFunction() 會等待 bStart 的旗標. 此旗標會由外部透過 CMasterThread::Start() 設定. 這樣的做法是可以讓外部準確的控制起跑時間.
  • 起跑之後, CMasterThread::ThreadFunction() 進入迴圈
    • bStop 旗標, 此旗標由外部控制是否繼續執行. 若不繼續執行, 則進行停止的程序.
    • 首先, 先取得一個 Frame
    • 以 TryEnterCriticalSection 嘗試鎖定 Computing 的 Buffer, 如果可以鎖定, 表示 Computing Thread 沒有在使用中, 所以可以更新資料. 如果不能鎖定, 表示 Computing Thread 在使用中. 放棄這次的更新.
    • 設定 Computing Thread 的 data available event. Computing Thread 會因而喚起, 開始處理.
    • 同樣的嘗試更新 Display Thread
  • 開始關閉程序
  • 設定 Shutdown event. Computing Thread 及 Display Thread 收到後會結束執行.
  • 等到 Computing Thread 及 Display Thread 都結束.
  • 關閉同步物件, 移除配置的記憶體.
  • 由外部呼叫 Clean() 釋放 CMasterThread 物件

** 注意, 程式中沒有資料處理, 而是以隨機的 Sleep() 來產生不同的時間差, 以測試程式的穩定性.

** Source code 的 plug-in 好像壞掉了. 可以由此取得完整專案 source code https://github.com/lxnick/ThreadTest

 

程式列表 :

MasterThread.h

[sourcecode language='cpp' ]
#include <process.h>
#include <windows.h>  
#include <string>

#define BUFFER_SIZE				(16 * 1024 * 1024 )

typedef struct tagTHREAD_INFO
{
	std::string szName;
	HANDLE		hHandle;
	DWORD		dwID;
	CRITICAL_SECTION	CriticalSection;
	HANDLE				hDataAvailable;

	unsigned long	nFrameIndex;
	unsigned char * pBuffer;
}	THREAD_INFO, *LPTHREAD_INFO;

class CMasterThread
{
private:
	CMasterThread();
	~CMasterThread();

	HANDLE hThread;	
	unsigned threadID; 

public:
	static CMasterThread* Create();
	static void Clean();

	static unsigned __stdcall ThreadFunction(void* pArguments);

	void Start();
	void Stop();

	BOOL bStart;
	BOOL bStop;

	HANDLE	hShutdown;

	THREAD_INFO	DisplayInfo;
	THREAD_INFO	ComputingInfo;
};
[/sourcecode]

MasterThread.cpp

[sourcecode language='cpp'  gutter='false']

#include "stdafx.h"
#include "MasterThread.h"

#define	WAIT_FRAME_TIME ( 100 )
#define	COMPUTING_TIME ( 50 )
#define	DISPLAY_TIME ( 50 )

const static char EVENT_COMPUTING[] = "ComputingDataAvailable";
const static char EVENT_DISPLAY[] = "DisplayDataAvailable";

static CMasterThread* pMasterThread = NULL;

unsigned __stdcall DisplayThreadFunction( void* pArguments )  
{
	LPTHREAD_INFO pInfo = (LPTHREAD_INFO) pArguments;

    printf( "\tDisplay Thread Launch ...\n" );  
	HANDLE hDataEvent = OpenEvent(EVENT_ALL_ACCESS, false, EVENT_DISPLAY);

	while( TRUE )
	{
		HANDLE Wait[] = { pMasterThread->hShutdown, pInfo->hDataAvailable };
		DWORD dwWait = WaitForMultipleObjects(sizeof(Wait)/sizeof(HANDLE), Wait, FALSE, INFINITE);

		if (dwWait != (WAIT_OBJECT_0 + 1))
		{
			printf("\tDisplay Thread Exit ...\n");
			return 0;
		}

		if ( TryEnterCriticalSection( & pInfo->CriticalSection ) != 0)
		{
			Sleep( DISPLAY_TIME );
			printf( "\tDisplay Thread Frame %d\n", pInfo->nFrameIndex);

			if (pInfo->pBuffer[0] != (pInfo->nFrameIndex & 0xFF))
				printf("\tDisplay Data dismatch\n");

			int random_number = rand() % DISPLAY_TIME;
			printf("\tDisplay Time = %d\n", random_number);
			Sleep(random_number);

			if (pInfo->pBuffer[0] != (pInfo->nFrameIndex & 0xFF))
				printf("\tDisplay Data overwritten\n");

			LeaveCriticalSection ( &pInfo->CriticalSection );
			ResetEvent(hDataEvent);
		}
	}

   printf( "\tDisplay Thread Shutdown ...\n" );  
}

unsigned __stdcall ComputingThreadFunction( void* pArguments )  
{
	LPTHREAD_INFO pInfo = (LPTHREAD_INFO) pArguments;

    printf( "\tComputing Thread Launch ...\n" );  
	HANDLE hDataEvent = OpenEvent(EVENT_ALL_ACCESS, false, EVENT_COMPUTING);

	while( TRUE )
	{
		HANDLE Wait[] = { pMasterThread->hShutdown, hDataEvent };
		DWORD dwWait = WaitForMultipleObjects(sizeof(Wait)/sizeof(HANDLE), Wait, FALSE, INFINITE);

		if ( dwWait != ( WAIT_OBJECT_0 +1))
		{
			printf("\tComputing Thread Exit ...\n");
			return 0;
		}

		if ( TryEnterCriticalSection( & pInfo->CriticalSection ) != 0)
		{
			printf("\tComputing Thread Frame %d\n", pInfo->nFrameIndex);

			if (pInfo->pBuffer[0] != (pInfo->nFrameIndex & 0xFF) )
				printf("\tComputing Data dismatch\n");

			int random_number = rand() % COMPUTING_TIME;
			printf("\tComputing Time = %d\n", random_number);
			Sleep(random_number);

			if (pInfo->pBuffer[0] != (pInfo->nFrameIndex & 0xFF))
				printf("\tComputing Data overwritten\n");

			LeaveCriticalSection(&pInfo->CriticalSection);
			ResetEvent(hDataEvent);
		}
	}
   printf( "\tComputing Thread Shutdown ...\n" ); 
}


unsigned __stdcall CMasterThread::ThreadFunction( void* pArguments )
{  
    printf( "Master Thread Launch ...\n" );  

	memset( &pMasterThread->DisplayInfo, 0, sizeof ( pMasterThread->DisplayInfo));
	pMasterThread->DisplayInfo.szName = "Display";
	InitializeCriticalSection( & pMasterThread->DisplayInfo.CriticalSection );
	pMasterThread->DisplayInfo.hDataAvailable = CreateEvent( NULL, TRUE, FALSE, EVENT_DISPLAY);
	pMasterThread->DisplayInfo.pBuffer = new unsigned char [ BUFFER_SIZE ];

	memset( &pMasterThread->ComputingInfo, 0, sizeof ( pMasterThread->ComputingInfo));
	pMasterThread->ComputingInfo.szName = "Display";
	InitializeCriticalSection( & pMasterThread->ComputingInfo.CriticalSection );
	pMasterThread->ComputingInfo.hDataAvailable = CreateEvent( NULL, TRUE, FALSE, EVENT_COMPUTING);
	pMasterThread->ComputingInfo.pBuffer = new unsigned char [ BUFFER_SIZE ];

	pMasterThread->hShutdown = CreateEvent( NULL, TRUE, FALSE, "MasterShutdown" );

	pMasterThread->DisplayInfo.hHandle =  (HANDLE)_beginthreadex( 
		NULL, 
		0, 
		&DisplayThreadFunction, 
		&pMasterThread->DisplayInfo, 
		0, 
		NULL );  

 	pMasterThread->ComputingInfo.hHandle =  (HANDLE)_beginthreadex( 
		NULL, 
		0, 
		&ComputingThreadFunction, 
		&pMasterThread->ComputingInfo, 
		0, 
		NULL );   

	unsigned char * pFrameBuffer = new unsigned char [ BUFFER_SIZE ];
	int FrameIndex = 0;

	while( ! pMasterThread->bStart)
		Sleep( 100 );

	while( ! pMasterThread->bStop )
	{
		int random_number = rand() % WAIT_FRAME_TIME;
		printf("Master Wait Frame = %d\n", random_number);
		Sleep(random_number);

		FrameIndex ++;
		printf( "Master Frame Index = %d\n", FrameIndex );  

		int FramePattern = FrameIndex & 0xFF;
		pFrameBuffer[0] = (unsigned char) FramePattern;
//		memset(pFrameBuffer, FramePattern, sizeof(unsigned char) * BUFFER_SIZE);

		if ( TryEnterCriticalSection( & pMasterThread->ComputingInfo.CriticalSection ) != 0)
		{
//			printf("Master Send to Computing = %d\n", FrameIndex);
			memcpy( pMasterThread->ComputingInfo.pBuffer, pFrameBuffer, sizeof( unsigned char ) * BUFFER_SIZE );
			pMasterThread->ComputingInfo.nFrameIndex = FrameIndex;
			LeaveCriticalSection ( & pMasterThread->ComputingInfo.CriticalSection );
			SetEvent( pMasterThread->ComputingInfo.hDataAvailable );
		}

		if ( TryEnterCriticalSection( & pMasterThread->DisplayInfo.CriticalSection ) != 0)
		{
//			printf("Master Send to Display = %d\n", FrameIndex);
			memcpy( pMasterThread->DisplayInfo.pBuffer, pFrameBuffer, sizeof( unsigned char ) * BUFFER_SIZE );
			pMasterThread->DisplayInfo.nFrameIndex = FrameIndex;
			LeaveCriticalSection ( & pMasterThread->DisplayInfo.CriticalSection );
			SetEvent( pMasterThread->DisplayInfo.hDataAvailable );
		}
	}

	printf( "Master Thread End Run\n");  
	SetEvent( pMasterThread->hShutdown );
	HANDLE hWaitThread[] = { pMasterThread->DisplayInfo.hHandle, pMasterThread->ComputingInfo.hHandle };
	WaitForMultipleObjects( sizeof(hWaitThread)/sizeof(HANDLE), hWaitThread, TRUE, INFINITE );
	printf("Child Thread Exit\n");

	CloseHandle( pMasterThread->hShutdown );
	CloseHandle( pMasterThread->ComputingInfo.hDataAvailable  );
	DeleteCriticalSection( & pMasterThread->ComputingInfo.CriticalSection );
	delete [] pMasterThread->ComputingInfo.pBuffer;
		 

	CloseHandle( pMasterThread->DisplayInfo.hDataAvailable );
	DeleteCriticalSection( & pMasterThread->DisplayInfo.CriticalSection );
	delete [] pMasterThread->DisplayInfo.pBuffer;

	ResetEvent(pMasterThread->hShutdown);
	CloseHandle(  pMasterThread->hShutdown);
	delete [] pFrameBuffer;

    return 0;  
}  



CMasterThread::CMasterThread()
{

}

CMasterThread::~CMasterThread()
{

}

CMasterThread* CMasterThread::Create()
{
	if ( pMasterThread != NULL)
		return pMasterThread;

	pMasterThread = new CMasterThread;
	pMasterThread->bStart = FALSE;
	pMasterThread->bStop = FALSE;

	pMasterThread->hThread =  (HANDLE)_beginthreadex( NULL, 0, &ThreadFunction, NULL, 0, &pMasterThread->threadID );

	return pMasterThread;
}

void CMasterThread::Clean()
{
	if ( pMasterThread != NULL)
		delete pMasterThread;

	pMasterThread = NULL;
}

void CMasterThread::Start()
{
	pMasterThread->bStart = TRUE;
}

void CMasterThread::Stop()
{
	pMasterThread->bStop = TRUE;
	WaitForSingleObject( pMasterThread->hThread, INFINITE );

	CloseHandle(pMasterThread->hThread);
}
[/sourcecode]

ThreadTest.cpp

[sourcecode language='cpp' ]
#include "stdafx.h"
#include <stdlib.h>  
#include <stdio.h>  
#include <time.h>  

#include "MasterThread.h"

int _tmain(int argc, _TCHAR* argv[])
{
	for (int i = 0; i < 100; i++)
	{
		srand((unsigned)time(NULL));

		CMasterThread * pMasterThread = CMasterThread::Create();
		Sleep(100);

		pMasterThread->Start();
		Sleep(1000 * 5);
		pMasterThread->Stop();

		CMasterThread::Clean();
	}

	return 0;
}
[/sourcecode]

2016年10月8日 星期六

[期貨] 台指期貨數據計算

以下整理台指的保證金及期交稅, 手續費

 

1. 保證金:

保證金分原始保證金及維持保證金.

原始保證金 83,000

維持保證金 64,000

2. 期交稅:

期交稅 37, 但是買賣都會收

3. 手續費:

手續費 100, 也是買賣都會收

4. 漲跌幅

指數 1 點的差額算 200 元.

5. 實例

10/7 日, 台指 12 月期. 開盤 9194, 最高 9202, 最低 9178, 收盤 9187.

以操作一口計 需要 83, 000 + 64, 000 = 147,000

 

如果在開盤買進, 然後在最高點平倉

獲利 (9202 – 9194 ) * 200 – 37*2 – 100 * 2 = 1,326

+ 0.9%

 

如果在開盤賣掉, 然後在最低點平倉

獲利 (9194 - 9178 ) * 200 – 37*2 – 100 * 2 = 2,953

+ 2.0 %

很有趣的結果. 參考參考.

[亂亂講] 四不總統 VS 六不總督

http://www.nownews.com/n/2016/10/07/2263684

 

總統蔡英文6日接受日本讀賣新聞專訪,這是她3天內第2度接受外媒專訪,主軸仍是兩岸關係。蔡英文繼續強調「4不」:承諾不變、善意不變、不屈服壓力,不走對抗老路.

 

這很有趣. 不由得想起英法戰爭時的兩廣總督葉名琛,面對英法聯軍兵臨城下, “不戰, 不和, 不守, 不死, 不降, 不走”. 最終被俘, 死在印度.

 

奈何.

2016年10月5日 星期三

[Util] 使用 Visual Studio 2015 下載 GitHub 的版本

1. 下載 repo

從 Visual Studio 的 File 選單, 選擇 File/New/Repository.

在 Visual Studio 的 Team Explorer 中, Local Repository 分頁, 選擇 Clone

image

填入 Git 的 URL, 例如 https://github.com/lxnick/Util.git, 此時 Local Repository 的路徑也已經做了相對的變化.

image

然後按下 Clone. 檔案就複製到 local.

2. 在 Visual Studio 中, 打開下載的專案.

先打開 Visual Studio 的 Team 分頁

image

選擇要打開的專案.  例如 Util. 在專案名稱上面按右鍵,  然後選擇 Open.

image

在 Solution 的選單下方, 選擇 Open. 在彈出的視窗中, 選擇 Util.sln. 回到 Solution Explorer 畫面, 專案已經打開.

[Util] 使用 Visual Studio 2015 的 git extension 連接 GitHub

介紹如何在 Visual Studio Community 2015 的 IDE 中, 連接上 GitHub. 現在 Visual Studio Community 2015 是免費的.

 

1. 首先在 GitHub 上建立一個 repository(Demo)

image

HTTPS 後面的, 就是可以存取到這個 repository 的網路位置.

image

2. 在本機端建立一個 github 的目錄.

image

3. 設定 Visual Studio 的 GitHub

在 Team Explorer 的分頁, 按下 Home 按紐, 以切換到 Home 分頁

image

選擇 Settings. 再選擇 Settings 下的 Global Settings. 填入 User name, email address, Default Repository Location 填入先前給定的路徑. 最後按下 Update 更新.

image

 

4. 在 Visual Studio 建立一個新的專案, Location 指向 先前給定的路徑.

image

完成後儲存專案.

image

專案檔案已生成在路徑中.

image

 

5.  將專案加入 Source Control

在 Solution Explorer 的分頁中. 在 Solution 名稱上按右鍵, 在彈出功能選單中, 選取 Add Solution to Source Control.

然後切換到 Team Explorer 畫面. 可以看到專案已被加入 local repository.

image

把 Enter a comment message 改掉 (譬如 Initial Version), Commit All 的顏色才會變成可以按下的狀態.

image

然後就 Commit All. 把變更加入 local repository.

6. 同步到 GitHub

在 Changes 分頁, 按下 Sync (藍色有底線的字 ). 切換到 Publish 頁面.

image

這個頁面有 3 個項目:

1. Publish to Visual Studio Team Services ( 這是 Microsoft 的 Team Server)

2. Publish to GitHub (這是我另外安裝的 Git extension for Visual Studio, 也可以用, 但不是這邊的重點)

3. Publish to Remote Repository (這是以下要說明的方法)

image

展開 Publish to Remote Repository, 在 “Enter the URL of an empty Git repo” , 代換成 GitHub 的 URL, 例如

“https://github.com/lxnick/Demo.git” 然後按下 Publish.

image

7. 完成. 回到 GitHub 的網頁. 檔案資料已經上傳.

image