⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xsortpub.h

📁 归并排序的并行算法 intel 多线程优化大赛
💻 H
字号:
/*/////////////////////////////////////////
// 版权所有(C)  2000-2008 邓辉           //
// Email:      denghui0815@hotmail.com  //
// 说明:       Intel线程优化大赛参赛作品//
/////////////////////////////////////////*/

#ifndef XSORTPUB_HEAD
#define XSORTPUB_HEAD

#include <mathimf.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <tmmintrin.h> 
#include <string.h>
#include <omp.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "tbb/tick_count.h" 
#include "tbb/scalable_allocator.h"
#include "tbb/task_scheduler_init.h"

#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/parallel_while.h"
#include "tbb/parallel_sort.h"
#include "tbb/parallel_reduce.h"

using namespace tbb;

#ifdef XCODE_WIN
	// 链接tbb
	#ifdef _DEBUG
		#pragma comment(lib, "tbb_debug.lib")
		#pragma comment(lib, "tbbmalloc_debug.lib")
	#else
		#pragma comment(lib, "tbb.lib")
		#pragma comment(lib, "tbbmalloc.lib")
	#endif

	#include <ipp.h>
	// 链接ipp
	#pragma comment(lib, "ipps.lib")
	#define XMEMCOPY(Macro_Src, Macro_Dst, Macro_Size)	\
		ippsCopy_8u( (const Ipp8u*)(Macro_Src), (Ipp8u*)(Macro_Dst), (Macro_Size) );
#endif

#ifdef XCODE_LINUX
	#define XMEMCOPY(Macro_Src, Macro_Dst, Macro_Size)	\
		memcpy( (Macro_Dst), (Macro_Src), (Macro_Size) );
#endif

typedef int                 			BOOL;
typedef unsigned char       			BYTE;

#ifndef FALSE
	#define FALSE						0
#endif

#ifndef TRUE
	#define TRUE						1
#endif

#ifndef MAX_PATH
	#define MAX_PATH					256
#endif

#pragma warning( disable : 1786)
#pragma warning( disable : 1684)

typedef unsigned int uint32;

typedef struct tagXSortArray
{
	uint32* pData;
	uint32  nData;
	uint32  nSize;
}XSortArray;

// 错误处理
void		Error(char *);
// 警告处理
void		Warning(char *);

// 创建数组
XSortArray*	XSortArrayCreate(uint32 nSize);
// 释放数组
void		XSortArrayDestroy(XSortArray** ppArray);
// 修改大小
void		XSortArrayResize(XSortArray* pArray, uint32 nNewSize);
// 校验排序结果
void		XSortArrayVerify(XSortArray* pArray);
// 构造随机数据
XSortArray* XSortArrayRand(uint32 nSize);
// 将pSrc1, pSrc2合并到pSrc1中
void		XSortArrayMerge(XSortArray* pSrc1, XSortArray* pSrc2);

// 加载测试数据
XSortArray*	XDataFileRead(const char* szFile);
// 输出计算结果
void		XDataFileSave(const char* szFile, XSortArray* pArray);
// 构造随机数据
void		XDataFileRand(uint32 nSize, const char* szFile);

// 二分查找定位
template<class Type>
int XSearch(Type Val, const Type* pArray, int nArray, BOOL bDesc)
{
	int nIndex = 0,l,u,m;

	l = -1,u = nArray;

	if (bDesc)
	{
		while (l + 1 != u)
		{
			m = (l + u )>>1;
			if (pArray[m] > Val)
				l = m;
			else
				u = m;
		}
	}
	else
	{
		while (l + 1 != u)
		{
			m = (l + u) >> 1;
			if (pArray[m] < Val)
				l = m;
			else
				u = m;
		}
	}
	
	if (u <= nArray) nIndex = u;

	return nIndex;
}

// 读取一个数字
//#define XREADNUM(pRead, pEnd, nRead)											\
{																				\
	nRead = *pRead++ - '0';														\
	while(*pRead >= '0' && *pRead <= '9')										\
	{																			\
		nRead = nRead * 10 + *pRead++ - '0';									\
	}																			\
	while(pRead < pEnd && (*pRead < '0' || *pRead > '9')) ++pRead;				\
}

#define XREADNUM(pRead, pEnd, nRead)											\
{																				\
	if(pRead[1] < '0' || pRead[1] > '9')										\
	{																			\
		nRead = pRead[0] - '0';													\
		pRead += 2;																\
	}																			\
	else if(pRead[2] < '0' || pRead[2] > '9')									\
	{																			\
		nRead = (pRead[0] - '0') * 10 + (pRead[1] - '0');						\
		pRead += 3;																\
	}																			\
	else if(pRead[3] < '0' || pRead[3] > '9')									\
	{																			\
		nRead = (pRead[0] - '0') * 100 + (pRead[1] - '0') * 10 +				\
				(pRead[2] - '0');												\
		pRead += 4;																\
	}																			\
	else if(pRead[4] < '0' || pRead[4] > '9')									\
	{																			\
		nRead = (pRead[0] - '0') * 1000 + (pRead[1] - '0') * 100 +				\
				(pRead[2] - '0') * 10 + (pRead[3] - '0') ;						\
		pRead += 5;																\
	}																			\
	else if(pRead[5] < '0' || pRead[5] > '9')									\
	{																			\
		nRead = (pRead[0] - '0') * 10000 + (pRead[1] - '0') * 1000 +			\
				(pRead[2] - '0') * 100 + (pRead[3] - '0') * 10 +				\
				(pRead[4] - '0');												\
		pRead += 6;																\
	}																			\
	else if(pRead[6] < '0' || pRead[6] > '9')									\
	{																			\
		nRead = (pRead[0] - '0') * 100000 + (pRead[1] - '0') * 10000 +			\
				(pRead[2] - '0') * 1000 + (pRead[3] - '0') * 100 +				\
				(pRead[4] - '0') * 10 + (pRead[5] - '0');						\
		pRead += 7;																\
	}																			\
	else if(pRead[7] < '0' || pRead[7] > '9')									\
	{																			\
		nRead = (pRead[0] - '0') * 1000000 + (pRead[1] - '0') * 100000 +		\
				(pRead[2] - '0') * 10000 + (pRead[3] - '0') * 1000 +			\
				(pRead[4] - '0') * 100 + (pRead[5] - '0') * 10 +				\
				(pRead[6] - '0');												\
		pRead += 8;																\
	}																			\
	else if(pRead[8] < '0' || pRead[8] > '9')									\
	{																			\
		nRead = (pRead[0] - '0') * 10000000 + (pRead[1] - '0') * 1000000 +		\
				(pRead[2] - '0') * 100000 + (pRead[3] - '0') * 10000 +			\
				(pRead[4] - '0') * 1000 + (pRead[5] - '0') * 100 +				\
				(pRead[6] - '0') * 10 + (pRead[7] - '0');						\
		pRead += 9;																\
	}																			\
	else if(pRead[9] < '0' || pRead[9] > '9')									\
	{																			\
		nRead = (pRead[0] - '0') * 100000000 + (pRead[1] - '0') * 10000000 +	\
				(pRead[2] - '0') * 1000000 + (pRead[3] - '0') * 100000 +		\
				(pRead[4] - '0') * 10000 + (pRead[5] - '0') * 1000 +			\
				(pRead[6] - '0') * 100 + (pRead[7] - '0') * 10 +				\
				(pRead[8] - '0');												\
		pRead += 10;															\
	}																			\
	else																		\
	{																			\
		nRead = (pRead[0] - '0') * 1000000000 + (pRead[1] - '0') * 100000000 +	\
				(pRead[2] - '0') * 10000000 + (pRead[3] - '0') * 1000000 +		\
				(pRead[4] - '0') * 100000 + (pRead[5] - '0') * 10000 +			\
				(pRead[6] - '0') * 1000 + (pRead[7] - '0') * 100 +				\
				(pRead[8] - '0') * 10 + (pRead[9] - '0');						\
		pRead += 11;															\
	}																			\
	while(pRead < pEnd && (*pRead < '0' || *pRead > '9')) ++pRead;				\
}

// 保存一个数字
//#define XSAVENUM(pSave, nSave)												\
{																				\
	char  pBuffer[16];															\
	char* pBufferPtr = pBuffer;													\
	while(nSave >= 10)															\
	{																			\
		*pBufferPtr++ = (nSave % 10) + '0';										\
		nSave /= 10;															\
	}																			\
	*pBufferPtr = (nSave % 10) + '0';											\
	while(pBufferPtr >= pBuffer) { *pSave++ = *pBufferPtr--;}					\
	*pSave++ = '\n';															\
}

//#define XSAVENUM(pSave, nSave)												\
{																				\
	ultoa(nSave, pSave, 10);													\
	while(*pSave != '\0') ++pSave;												\
	*pSave++ = '\n';															\
}

#define XSAVENUM_ITEM(pSave, nSave,  nIndex) pSave[nIndex] = (nSave % 10) + '0'; nSave /= 10;
#define XSAVENUM_END(pSave, nSave,   nCount) pSave[0] = (nSave % 10) + '0'; pSave[nCount] = '\n';	pSave += nCount + 1; 
#define XSAVENUM(pSave, nSave)													\
{																				\
	if(nSave > 1000000000)														\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 9)											\
		XSAVENUM_ITEM(pSave, nSave, 8)											\
		XSAVENUM_ITEM(pSave, nSave, 7)											\
		XSAVENUM_ITEM(pSave, nSave, 6)											\
		XSAVENUM_ITEM(pSave, nSave, 5)											\
		XSAVENUM_ITEM(pSave, nSave, 4)											\
		XSAVENUM_ITEM(pSave, nSave, 3)											\
		XSAVENUM_ITEM(pSave, nSave, 2)											\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 10)											\
	}																			\
	else if(nSave > 100000000)													\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 8)											\
		XSAVENUM_ITEM(pSave, nSave, 7)											\
		XSAVENUM_ITEM(pSave, nSave, 6)											\
		XSAVENUM_ITEM(pSave, nSave, 5)											\
		XSAVENUM_ITEM(pSave, nSave, 4)											\
		XSAVENUM_ITEM(pSave, nSave, 3)											\
		XSAVENUM_ITEM(pSave, nSave, 2)											\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 9)											\
	}																			\
	else if(nSave > 10000000)													\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 7)											\
		XSAVENUM_ITEM(pSave, nSave, 6)											\
		XSAVENUM_ITEM(pSave, nSave, 5)											\
		XSAVENUM_ITEM(pSave, nSave, 4)											\
		XSAVENUM_ITEM(pSave, nSave, 3)											\
		XSAVENUM_ITEM(pSave, nSave, 2)											\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 8)											\
	}																			\
	else if(nSave > 1000000)													\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 6)											\
		XSAVENUM_ITEM(pSave, nSave, 5)											\
		XSAVENUM_ITEM(pSave, nSave, 4)											\
		XSAVENUM_ITEM(pSave, nSave, 3)											\
		XSAVENUM_ITEM(pSave, nSave, 2)											\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 7)											\
	}																			\
	else if(nSave > 100000)														\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 5)											\
		XSAVENUM_ITEM(pSave, nSave, 4)											\
		XSAVENUM_ITEM(pSave, nSave, 3)											\
		XSAVENUM_ITEM(pSave, nSave, 2)											\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 6)											\
	}																			\
	else if(nSave > 10000)														\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 4)											\
		XSAVENUM_ITEM(pSave, nSave, 3)											\
		XSAVENUM_ITEM(pSave, nSave, 2)											\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 5)											\
	}																			\
	else if(nSave > 1000)														\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 3)											\
		XSAVENUM_ITEM(pSave, nSave, 2)											\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 4)											\
	}																			\
	else if(nSave > 100)														\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 2)											\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 3)											\
	}																			\
	else if(nSave > 10)															\
	{																			\
		XSAVENUM_ITEM(pSave, nSave, 1)											\
		XSAVENUM_END (pSave, nSave, 2)											\
	}																			\
	else																		\
	{																			\
		XSAVENUM_END (pSave, nSave, 1)											\
	}																			\
}

#endif																			
																				

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -