Пример #1
0
void LaserScanMatcher::scanCallback (const sensor_msgs::LaserScan::ConstPtr& scan_msg)
{
  // **** if first scan, cache the tf from base to the scanner
  
  if (!initialized_)
  {
    createCache(scan_msg);    // caches the sin and cos of all angles

    // cache the static tf from base to laser
    if (!getBaseToLaserTf(scan_msg->header.frame_id))
    {
		ROS_INFO("FRAME_ID : %s",scan_msg->header.frame_id.c_str());
      ROS_WARN("Skipping laser scan");
      return;
    }

    laserScanToLDP(scan_msg, prev_ldp_scan_); 
    last_icp_time_ = scan_msg->header.stamp;
    initialized_ = true;
  }

  LDP curr_ldp_scan;
  laserScanToLDP(scan_msg, curr_ldp_scan);
  processScan(curr_ldp_scan, scan_msg->header.stamp);
}
Пример #2
0
/* plugin methods */
int
postgresInitMetadataLookupPlugin (void)
{
    PostgresMetaCache *psqlCache;

    if (plugin && plugin->initialized)
    {
        INFO_LOG("tried to initialize metadata lookup plugin more than once");
        return EXIT_SUCCESS;
    }

    NEW_AND_ACQUIRE_MEMCONTEXT(CONTEXT_NAME);
    memContext = getCurMemContext();

    // create cache
    plugin->plugin.cache = createCache();

    // create postgres specific part of the cache
    psqlCache = NEW(PostgresMetaCache);
    psqlCache->oidToDT = NEW_MAP(Constant,Constant);
    plugin->plugin.cache->cacheHook = (void *) psqlCache;

    plugin->initialized = TRUE;

    RELEASE_MEM_CONTEXT();
    return EXIT_SUCCESS;
}
 void insert(std::pair<std::string, _Type*>&& item)
 {
     createCache();
     if (!_mutex) _mutex = new FastMutex();
     FastMutex::ScopedLock _(*_mutex);
     _instance->insert(std::move(item));
 }
Пример #4
0
void createSearch()
{
    char* cachesize = "0KB";
        
    /* Create a tokenizer */
    tok = TKCreate(FILE_CHARS, "myindex.txt");
    if(tok == NULL)
    {
        fprintf(stderr, "Error: Could not allocate space for Tokenizer.\n");
        return;
    }
    
    /* Get the file list */
    files = getFilelist(tok);
    if(files == NULL)
    {
        return;
    }
    
    /* Create a cache */
    cache = createCache(cachesize);
    if(cache == NULL)
    {
        fprintf(stderr, "Error: Could not allocate space for Cache.\n");
        return;
    }
    
    /* Update the allowed characters */
    adjustAllowedChars(tok, STRING_CHARS);
}
Пример #5
0
void QNetworkReplyImplPrivate::setCachingEnabled(bool enable)
{
    if (!enable && !cacheEnabled)
        return;                 // nothing to do
    if (enable && cacheEnabled)
        return;                 // nothing to do either!

    if (enable) {
        if (bytesDownloaded) {
            // refuse to enable in this case
            qCritical("QNetworkReplyImpl: backend error: caching was enabled after some bytes had been written");
            return;
        }

        createCache();
    } else {
        // someone told us to turn on, then back off?
        // ok... but you should make up your mind
        qDebug("QNetworkReplyImpl: setCachingEnabled(true) called after setCachingEnabled(false) -- "
               "backend %s probably needs to be fixed",
               backend->metaObject()->className());
        networkCache()->remove(url);
        cacheSaveDevice = 0;
        cacheEnabled = false;
    }
}
 void insert(std::string const& key, _Type* const& value)
 {
     createCache();
     std::pair<std::string, _Type*> item(key, value);
     if (!_mutex) _mutex = new FastMutex();
     FastMutex::ScopedLock _(*_mutex);
     _instance->insert(item);
 }
 void insert(std::string&& key, _Type*&& value)
 {
     createCache();
     std::pair<std::string, _Type*> item(std::move(key), std::move(value));
     value = NULL;
     if (!_mutex) _mutex = new FastMutex();
     FastMutex::ScopedLock _(*_mutex);
     _instance->insert(item);
 }
 void insert(_Type* const& value)
 {
     createCache();
     char buff[20] = { 0 };
     sprintf(buff, "%lx", (unsigned long)value);
     std::pair<std::string, _Type*> item(buff, value);
     if (!_mutex) _mutex = new FastMutex();
     FastMutex::ScopedLock _(*_mutex);
     _instance->insert(item);
 }
/*---------------------------------------------------------------------*//**
	コンストラクタ
**//*---------------------------------------------------------------------*/
FileAndroidLargeAsset::FileAndroidLargeAsset(FILE_MNG_R* astmng)
	: File()
	, _astmng(astmng)
	, _pathFileOrg(new VcString())
	, _oaarr(0L)
	, _offset(0)
	, _numCache(NUM_OPENED_CACHE_DEFAULT)
	, _isOpened(false)
{
	createCache();
}
Пример #10
0
  CacheFixture() : objWritten_(0), currSize_(0)
  {
    std::vector< uint8_t > key;

    const std::string dummykey( "dummykey" );
    std::copy( dummykey.begin(), dummykey.end(), back_inserter( key ) );
    BOOST_TEST_MESSAGE( "Creating Cache instance" );
    cache_ = createCache( "c:\\temp\\cache", key );;
    BOOST_REQUIRE( cache_ );
    cache_->setMaxSize( maxSize );
  }
/*---------------------------------------------------------------------*//**
	コンストラクタ
**//*---------------------------------------------------------------------*/
FileAndroidLargeAsset::FileAndroidLargeAsset(FILE_MNG_R* astmng, s32 numCache)
	: File()
	, _astmng(astmng)
	, _pathFileOrg(new VcString())
	, _oaarr(0L)
	, _offset(0)
	, _numCache(numCache)
	, _isOpened(false)
{
	createCache();
}
 std::auto_ptr<Interfaces::IAggregate<StringTypeMap::key_type> > getKeyAggregate()
 {
     createCache();
     if (!_mutex) _mutex = new FastMutex();
     FastMutex::ScopedLock _(*_mutex);
     typedef Patterns::StdContainerAggregate<StringTypeMap*,
                                             StringTypeMap::key_type,
                                             StringTypeMap::const_iterator,
                                             KeyConverter,
                                             KeyRange> result_t;
     return std::auto_ptr<Interfaces::IAggregate<StringTypeMap::key_type> >(new result_t(_instance));
 }
Пример #13
0
void cacheTesting(){
    Cache* cache = createCache(10, 4);
    int cacheLineNum;
    bool compute;
    cacheCall(cache, 9, &cacheLineNum, &compute);
    cacheCall(cache, 0, &cacheLineNum, &compute);
    cacheCall(cache, 2, &cacheLineNum, &compute);
    cacheCall(cache, 2, &cacheLineNum, &compute);
    cacheCall(cache, 1, &cacheLineNum, &compute);
    cacheCall(cache, 9, &cacheLineNum, &compute);
    cacheCall(cache, 3, &cacheLineNum, &compute);
    printf("%d%d\n",cacheLineNum,compute);
}
Пример #14
0
int initKMalloc(void)
{
   for(int i = 0;i < sizeof(mallocSizes)/sizeof(MallocSize);++i)
   {
      mallocSizes[i].cache = createCache(mallocSizes[i].size,0x0);
      if(unlikely(!mallocSizes[i].cache))
      {
         printkInColor(0xff,0x00,0x00,"(%s) Can't get memorySize[%d].cache!",__func__,i);
         return -ENOMEM;
      }
   }
   return 0;
}
/*---------------------------------------------------------------------*//**
	コンストラクタ
**//*---------------------------------------------------------------------*/
FileAndroidLargeAsset::FileAndroidLargeAsset(FILE_MNG_R* astmng, CcString pathFile, Mode mode, s32 numCache)
	:  File(pathFile, mode)
	, _astmng(astmng)
	, _pathFileOrg(new VcString())
	, _oaarr(0L)
	, _offset(0)
	, _numCache(numCache)
	, _isOpened(false)
{
	if(createCache())
	{
		open(&pathFile, mode);
	}
}
Пример #16
0
MojErr MojDbSearchCache::updateCache(const QueryKey& a_key, const IdSet& a_ids)
{
    // Skip updating the cache ONLY if up-to-date cache is available.
    //
    if (contain(a_key) == true)
        return MojErrNone;

    // Delete any cache for this query/kind here.
    //
    MojErr err = destroyCache(a_key.getKind());
    MojErrCheck(err);

    // Add new cache.
    //
    err = createCache(a_key, a_ids);
    MojErrCheck(err);

    return MojErrNone;
}
        std::unique_ptr<Interfaces::IAggregate<typename StringTypeMap::key_type> > getKeyAggregate()
#else
        std::auto_ptr<Interfaces::IAggregate<typename StringTypeMap::key_type> > getKeyAggregate()
#endif
        {
            createCache();
            if (!_mutex) _mutex = new FastMutex();
            FastMutex::ScopedLock _(*_mutex);
            typedef Patterns::StdContainerAggregate<StringTypeMap*,
                                                    typename StringTypeMap::key_type,
                                                    typename StringTypeMap::const_iterator,
                                                    KeyConverter,
                                                    KeyRange> result_t;
#if defined(PUREMVC_USES_TR1)
            return std::unique_ptr<Interfaces::IAggregate<typename StringTypeMap::key_type> >(new result_t(_instance));
#else
            return std::auto_ptr<Interfaces::IAggregate<typename StringTypeMap::key_type> >(new result_t(_instance));
#endif
        }
void QgsValueRelationWidgetWrapper::initWidget( QWidget* editor )
{
  mCache = createCache( config() );

  mComboBox = qobject_cast<QComboBox*>( editor );
  mListWidget = qobject_cast<QListWidget*>( editor );
  mLineEdit = qobject_cast<QLineEdit*>( editor );

  if ( mComboBox )
  {
    if ( config( "AllowNull" ).toBool() )
    {
      mComboBox->addItem( tr( "(no selection)" ), QVariant( field().type() ) );
    }

    Q_FOREACH ( const ValueRelationItem& element, mCache )
    {
      mComboBox->addItem( element.second, element.first );
    }

    connect( mComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( valueChanged() ) );
  }
Пример #19
0
void QTextureGlyphCache::fillInPendingGlyphs()
{
    if (m_pendingGlyphs.isEmpty())
        return;

    int requiredHeight = m_h;
    int requiredWidth = m_w; // Use a minimum size to avoid a lot of initial reallocations
    {
        QHash<GlyphAndSubPixelPosition, Coord>::iterator iter = m_pendingGlyphs.begin();
        while (iter != m_pendingGlyphs.end()) {
            Coord c = iter.value();
            requiredHeight = qMax(requiredHeight, c.y + c.h);
            requiredWidth = qMax(requiredWidth, c.x + c.w);
            ++iter;
        }
    }

    if (isNull() || requiredHeight > m_h || requiredWidth > m_w) {
        if (isNull())
            createCache(qt_next_power_of_two(requiredWidth), qt_next_power_of_two(requiredHeight));
        else
            resizeCache(qt_next_power_of_two(requiredWidth), qt_next_power_of_two(requiredHeight));
    }

    {
        QHash<GlyphAndSubPixelPosition, Coord>::iterator iter = m_pendingGlyphs.begin();
        while (iter != m_pendingGlyphs.end()) {
            GlyphAndSubPixelPosition key = iter.key();
            fillTexture(iter.value(), key.glyph, key.subPixelPosition);

            ++iter;
        }
    }

    m_pendingGlyphs.clear();
}
Пример #20
0
CacheManager::CacheManager()
{
	disabled=true;
	cache=createCache();
}
Пример #21
0
void CacheManager::flushCaches()
{
	delete cache;
	cache=createCache();
}
Пример #22
0
CacheManager::CacheManager() :
	disabled(true)
{
	cache=createCache();
}
Пример #23
0
void HistogramFilter::createCellHistograms(const Mat& image, Mat& histograms, int binCount, int rowCount, int columnCount, bool interpolate) const {
	if (image.channels() != 1 && image.channels() != 2 && image.channels() != 4)
		throw runtime_error("HistogramFilter: image must have one, two or four channels");
	if (image.depth() != CV_8U)
		throw runtime_error("HistogramFilter: image must have a depth of CV_8U");

	histograms = Mat::zeros(rowCount, columnCount, CV_32FC(binCount));
	float factor = 1.f / 255.f;
	if (interpolate) { // bilinear interpolation between cells
		createCache(rowCache, image.rows, rowCount);
		createCache(colCache, image.cols, columnCount);
		if (image.channels() == 1) { // bin information only, no weights
			for (int imageRow = 0; imageRow < image.rows; ++imageRow) {
				const uchar* rowValues = image.ptr<uchar>(imageRow);
				int rowIndex0 = rowCache[imageRow].index1;
				int rowIndex1 = rowCache[imageRow].index2;
				float rowWeight1 = rowCache[imageRow].weight2;
				float rowWeight0 = rowCache[imageRow].weight1;
				for (int imageCol = 0; imageCol < image.cols; ++imageCol) {
					uchar bin = rowValues[imageCol];

					int colIndex0 = colCache[imageCol].index1;
					int colIndex1 = colCache[imageCol].index2;
					float colWeight1 = colCache[imageCol].weight2;
					float colWeight0 = colCache[imageCol].weight1;
					if (rowIndex0 >= 0 && colIndex0 >= 0) {
						float* histogramValues = histograms.ptr<float>(rowIndex0, colIndex0);
						histogramValues[bin] += rowWeight0 * colWeight0;
					}
					if (rowIndex0 >= 0 && colIndex1 < columnCount) {
						float* histogramValues = histograms.ptr<float>(rowIndex0, colIndex1);
						histogramValues[bin] += rowWeight0 * colWeight1;
					}
					if (rowIndex1 < rowCount && colIndex0 >= 0) {
						float* histogramValues = histograms.ptr<float>(rowIndex1, colIndex0);
						histogramValues[bin] += rowWeight1 * colWeight0;
					}
					if (rowIndex1 < rowCount && colIndex1 < columnCount) {
						float* histogramValues = histograms.ptr<float>(rowIndex1, colIndex1);
						histogramValues[bin] += rowWeight1 * colWeight1;
					}
				}
			}
		} else if (image.channels() == 2) { // bin index and weight available
			for (int imageRow = 0; imageRow < image.rows; ++imageRow) {
				const Vec2b* rowValues = image.ptr<Vec2b>(imageRow);
				int rowIndex0 = rowCache[imageRow].index1;
				int rowIndex1 = rowCache[imageRow].index2;
				float rowWeight1 = rowCache[imageRow].weight2;
				float rowWeight0 = rowCache[imageRow].weight1;
				for (int imageCol = 0; imageCol < image.cols; ++imageCol) {
					uchar bin = rowValues[imageCol][0];
					float weight = factor * rowValues[imageCol][1];

					int colIndex0 = colCache[imageCol].index1;
					int colIndex1 = colCache[imageCol].index2;
					float colWeight1 = colCache[imageCol].weight2;
					float colWeight0 = colCache[imageCol].weight1;
					if (rowIndex0 >= 0 && colIndex0 >= 0) {
						float* histogramValues = histograms.ptr<float>(rowIndex0, colIndex0);
						histogramValues[bin] += weight * rowWeight0 * colWeight0;
					}
					if (rowIndex0 >= 0 && colIndex1 < columnCount) {
						float* histogramValues = histograms.ptr<float>(rowIndex0, colIndex1);
						histogramValues[bin] += weight * rowWeight0 * colWeight1;
					}
					if (rowIndex1 < rowCount && colIndex0 >= 0) {
						float* histogramValues = histograms.ptr<float>(rowIndex1, colIndex0);
						histogramValues[bin] += weight * rowWeight1 * colWeight0;
					}
					if (rowIndex1 < rowCount && colIndex1 < columnCount) {
						float* histogramValues = histograms.ptr<float>(rowIndex1, colIndex1);
						histogramValues[bin] += weight * rowWeight1 * colWeight1;
					}
				}
			}
		} else if (image.channels() == 4) { // two bin indices and weights available
			for (int imageRow = 0; imageRow < image.rows; ++imageRow) {
				const Vec4b* rowValues = image.ptr<Vec4b>(imageRow);
				int rowIndex0 = rowCache[imageRow].index1;
				int rowIndex1 = rowCache[imageRow].index2;
				float rowWeight1 = rowCache[imageRow].weight2;
				float rowWeight0 = rowCache[imageRow].weight1;
				for (int imageCol = 0; imageCol < image.cols; ++imageCol) {
					uchar bin1 = rowValues[imageCol][0];
					float weight1 = factor * rowValues[imageCol][1];
					uchar bin2 = rowValues[imageCol][2];
					float weight2 = factor * rowValues[imageCol][3];

					int colIndex0 = colCache[imageCol].index1;
					int colIndex1 = colCache[imageCol].index2;
					float colWeight1 = colCache[imageCol].weight2;
					float colWeight0 = colCache[imageCol].weight1;
					if (rowIndex0 >= 0 && colIndex0 >= 0) {
						float* histogramValues = histograms.ptr<float>(rowIndex0, colIndex0);
						histogramValues[bin1] += weight1 * rowWeight0 * colWeight0;
						histogramValues[bin2] += weight2 * rowWeight0 * colWeight0;
					}
					if (rowIndex0 >= 0 && colIndex1 < columnCount) {
						float* histogramValues = histograms.ptr<float>(rowIndex0, colIndex1);
						histogramValues[bin1] += weight1 * rowWeight0 * colWeight1;
						histogramValues[bin2] += weight2 * rowWeight0 * colWeight1;
					}
					if (rowIndex1 < rowCount && colIndex0 >= 0) {
						float* histogramValues = histograms.ptr<float>(rowIndex1, colIndex0);
						histogramValues[bin1] += weight1 * rowWeight1 * colWeight0;
						histogramValues[bin2] += weight2 * rowWeight1 * colWeight0;
					}
					if (rowIndex1 < rowCount && colIndex1 < columnCount) {
						float* histogramValues = histograms.ptr<float>(rowIndex1, colIndex1);
						histogramValues[bin1] += weight1 * rowWeight1 * colWeight1;
						histogramValues[bin2] += weight2 * rowWeight1 * colWeight1;
					}
				}
			}
		}
	} else { // no bilinear interpolation between cells
		float* histogramValues = histograms.ptr<float>();
		if (image.channels() == 1) { // bin information only, no weights
			for (int cellRow = 0; cellRow < rowCount; ++cellRow) {
				for (int cellCol = 0; cellCol < columnCount; ++cellCol) {
					int startRow = (cellRow * image.rows) / rowCount;
					int startCol = (cellCol * image.cols) / columnCount;
					int endRow = ((cellRow + 1) * image.rows) / rowCount;
					int endCol = ((cellCol + 1) * image.cols) / columnCount;
					for (int imageRow = startRow; imageRow < endRow; ++imageRow) {
						const uchar* rowValues = image.ptr<uchar>(imageRow);
						for (int imageCol = startCol; imageCol < endCol; ++imageCol)
							histogramValues[rowValues[imageCol]]++;
					}
					histogramValues += binCount;
				}
			}
		} else if (image.channels() == 2) { // bin index and weight available
			for (int cellRow = 0; cellRow < rowCount; ++cellRow) {
				for (int cellCol = 0; cellCol < columnCount; ++cellCol) {
					int startRow = (cellRow * image.rows) / rowCount;
					int startCol = (cellCol * image.cols) / columnCount;
					int endRow = ((cellRow + 1) * image.rows) / rowCount;
					int endCol = ((cellCol + 1) * image.cols) / columnCount;
					for (int imageRow = startRow; imageRow < endRow; ++imageRow) {
						const Vec2b* rowValues = image.ptr<Vec2b>(imageRow);
						for (int imageCol = startCol; imageCol < endCol; ++imageCol) {
							uchar bin = rowValues[imageCol][0];
							uchar weight = rowValues[imageCol][1];
							histogramValues[bin] += factor * weight;
						}
					}
					histogramValues += binCount;
				}
			}
		} else if (image.channels() == 4) { // two bin indices and weights available
			for (int cellRow = 0; cellRow < rowCount; ++cellRow) {
				for (int cellCol = 0; cellCol < columnCount; ++cellCol) {
					int startRow = (cellRow * image.rows) / rowCount;
					int startCol = (cellCol * image.cols) / columnCount;
					int endRow = ((cellRow + 1) * image.rows) / rowCount;
					int endCol = ((cellCol + 1) * image.cols) / columnCount;
					for (int imageRow = startRow; imageRow < endRow; ++imageRow) {
						const Vec4b* rowValues = image.ptr<Vec4b>(imageRow);
						for (int imageCol = startCol; imageCol < endCol; ++imageCol) {
							uchar bin1 = rowValues[imageCol][0];
							uchar weight1 = rowValues[imageCol][1];
							uchar bin2 = rowValues[imageCol][2];
							uchar weight2 = rowValues[imageCol][3];
							histogramValues[bin1] += factor * weight1;
							histogramValues[bin2] += factor * weight2;
						}
					}
					histogramValues += binCount;
				}
			}
		}
	}
}
Пример #24
0
int DcmFileProcess::readAllDcm(const char* FilePath, std::vector<float>& position)
{
	//构造类对象  
	CStatDir statdir;
	std::vector<std::string> AllDcmFile;

	position.clear();
	float min_axial = 1000.0f;
	float max_axial = -1000.0f;
	float init_x = 0.0f;
	float init_y = 0.0f;
	float pixelspacing = 0.0f;

	int count = 0;
	//设置要遍历的目录  
	if (!statdir.SetInitDir(FilePath))
	{
		puts("目录不存在");
	}
	//开始遍历,获取该文件夹下所有dcm格式图像,一般为一个病患
	AllDcmFile.clear();
	AllDcmFile = statdir.BeginBrowseFilenames("*.dcm");
	//创建缓冲文件夹
	createCache();
	std::string dirName = GetExePath();
	dirName += "\\cache\\";

	cv::Mat mat[710];

	// 获取原始dcm图像的前缀目录
	std::string subImageName = dirName;
	// 获取原始dcm图像的前缀目录的string转换为char*
	const char *chSubImageName = subImageName.c_str();

	for (auto iter = AllDcmFile.cbegin(); iter != AllDcmFile.cend(); iter++)
	{
		// 将原始dcm图像的string转换为char*
		const char *chImageName = (*iter).c_str();
		// 基于dcmtk实现类
		THU_STD_NAMESPACE::TDcmFileFormat dcm = THU_STD_NAMESPACE::TDcmFileFormat(chImageName);

		std::string result = dcm.getImagePositionPatient();
		std::vector<std::string> ImagePosition;
		split(result, "\\", ImagePosition);
		float axial = (float)atof(ImagePosition[2].c_str());
		if (axial > max_axial) max_axial = axial;
		if (axial < min_axial) min_axial = axial;
		// 得到世界坐标的初始值
		if (count < 1)
		{
			init_x = (float)atof(ImagePosition[0].c_str());
			init_y = (float)atof(ImagePosition[1].c_str());

			// 获取dcm图像的PixelSpacing
			std::string space = dcm.getPixelSpacing();
			std::vector<std::string> PixelSpacing;
			split(space, "\\", PixelSpacing);
			pixelspacing = (float)atof(PixelSpacing[0].c_str());
		}
		
		// 获取dcm图像的InstanceNumber
		int InstancePosition = dcm.getPositionNumber();
		// 生成bmp图像存储路径
		char BmpName[256];
		sprintf_s(BmpName, "%s%06d.bmp", chSubImageName, InstancePosition);
		// 进行图像转换
		dcm.setWindow(715, 3478);
		dcm.saveToBmp(BmpName);
		//读取所有dcm图像构造三维数组
		cv::Mat temp = cv::imread(BmpName, cv::IMREAD_GRAYSCALE);
		mat[InstancePosition - 1] = temp;

		count++;
	}

	position.push_back(max_axial);
	position.push_back(min_axial);
	position.push_back((max_axial - min_axial) / count);
	position.push_back(init_x);
	position.push_back(init_y);
	position.push_back(pixelspacing);

	//多线程需要处理的线程数,分别对应矢状位与冠状位
	const int THREAD_NUM = 2;
	HANDLE handle[THREAD_NUM];
	//构造子线程参数结构体
	ThreadInfo threadInfo;
	threadInfo.dirName = dirName;
	threadInfo.count = count;
	for (int i = 0; i < count; i++)
		threadInfo.mat[i] = mat[i];
	//Coronal子线程
	handle[0] = (HANDLE)_beginthreadex(NULL, 0, ThreadCoronal, &threadInfo, 0, NULL);
	//Sigattal子线程
	handle[1] = (HANDLE)_beginthreadex(NULL, 0, ThreadSagittal, &threadInfo, 0, NULL);
	WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE);

	return count;
}
Пример #25
0
int train(int cacheSizeInFloats, float *input_data, int *labels,
            float epsilon, float Ce, float cost, float tolerance,
            int heuristic, int nPoints,  int dFeatures,
            float paramA, float paramB, float paramC,
            size_t localInitSize, size_t globalInitSize,
            int numGroups_foph1, size_t localFoph1Size, size_t globalFoph1Size,
            size_t localFoph2Size, size_t globalFoph2Size,
            int numGroups_soph1, size_t localSoph1Size, size_t globalSoph1Size,
            size_t localSoph2Size, size_t globalSoph2Size,
            int numGroups_soph3, size_t localSoph3Size, size_t globalSoph3Size,
            size_t localSoph4Size, size_t globalSoph4Size,
            cl_mem d_input_data, cl_mem d_input_data_colmajor, cl_mem d_labels, cl_mem d_trainingAlpha, cl_mem d_kernelDiag, cl_mem d_F,
            cl_mem d_highFsFO, cl_mem d_highIndicesFO, cl_mem d_lowFsFO, cl_mem d_lowIndicesFO,
            cl_mem d_highFsSO1, cl_mem d_highIndicesSO1, cl_mem d_lowFsSO3, cl_mem d_lowIndicesSO3, cl_mem d_deltaFsSO3,
            cl_mem d_results, cl_mem d_cache,
            cl_command_queue queue, cl_kernel init, cl_kernel step1, cl_kernel foph1, cl_kernel foph2,
            cl_kernel soph1, cl_kernel soph2, cl_kernel soph3, cl_kernel soph4,
            float *p_rho, int *p_nSV, int *p_iterations,
            float **p_signedAlpha, float **p_supportVectors){
    int numCacheLines = cacheSizeInFloats/nPoints;
    Cache *cache = createCache(nPoints,numCacheLines);
    Controller *progress = createController(2.0, 64 ,nPoints);
    int err;
    err = 0;
    err |= clSetKernelArg(init, 0, sizeof(cl_mem), &d_input_data);
    err |= clSetKernelArg(init, 1, sizeof(cl_mem), &d_labels);
    err |= clSetKernelArg(init, 2, sizeof(cl_mem), &d_F);
    err |= clSetKernelArg(init, 3, sizeof(cl_mem), &d_kernelDiag);
    err |= clSetKernelArg(init, 4, sizeof(int), &nPoints);
    err |= clSetKernelArg(init, 5, sizeof(int), &dFeatures);
    err |= clSetKernelArg(init, 6, sizeof(float), &paramA);
    err |= clSetKernelArg(init, 7, sizeof(float), &paramB);
    err |= clSetKernelArg(init, 8, sizeof(float), &paramC);
    if (err != CL_SUCCESS){
                printf("Error: Failed to set kernel arguments! %d\n", err);
                return err;
    }
    err = clEnqueueNDRangeKernel(queue, init, 1, NULL, &globalInitSize, &localInitSize, 0, NULL, NULL);
    if (err != CL_SUCCESS){
        printf("Error: Failed to execute kernel init!\n");
        printf("Global Size:%zu, Local Size:%zu\n", globalInitSize, localInitSize);

        return err;
    }



    float bLow;
    float bHigh;
    int iLow;
    int iHigh;
    float alpha1diff;
    float alpha2diff;
    size_t globalStep1Size = 1;
    size_t localStep1Size = 1;
    err = 0;
    err |= clSetKernelArg(step1, 0, sizeof(cl_mem), &d_input_data);
    err |= clSetKernelArg(step1, 1, sizeof(cl_mem), &d_labels);
    err |= clSetKernelArg(step1, 2, sizeof(cl_mem), &d_trainingAlpha);
    err |= clSetKernelArg(step1, 3, sizeof(cl_mem), &d_kernelDiag);
    err |= clSetKernelArg(step1, 4, sizeof(float), &cost);
    err |= clSetKernelArg(step1, 5, sizeof(int), &nPoints);
    err |= clSetKernelArg(step1, 6, sizeof(int), &dFeatures);
    err |= clSetKernelArg(step1, 7, sizeof(float), &paramA);
    err |= clSetKernelArg(step1, 8, sizeof(float), &paramB);
    err |= clSetKernelArg(step1, 9, sizeof(float), &paramC);
    err |= clSetKernelArg(step1, 10, sizeof(cl_mem), &d_results);


    if (err != CL_SUCCESS){
        printf("Error: Failed to set kernel arguments! %d\n", err);
        return err;
    }
    err = clEnqueueNDRangeKernel(queue, step1, 1, NULL, &globalStep1Size, &localStep1Size, 0, NULL, NULL);
    if (err != CL_SUCCESS){
        printf("Error: Failed to execute kernel step1!\n");
        return err;
    }
    err = clFinish(queue);
    if (err != CL_SUCCESS){
        printf("Error: waiting for queue to finish failed\n");
        return err;
    }

    float results[8];
    getResults(queue, d_results, results, &bLow, &bHigh, &iLow, &iHigh, &alpha1diff, &alpha2diff);

    int iteration;
    bool iLowCompute;
    bool iHighCompute;
    int iLowCacheIndex;
    int iHighCacheIndex;
    int currentHeuristic;
//    clock_t startTot = clock(), diffTot, diffSoph1, diffSoph2, diffSoph3, diffSoph4, diffGetResults;

    for (iteration = 0; true; iteration++){
        if(heuristic == 2){
            currentHeuristic = progress->heuristic;
        }else{
            currentHeuristic = heuristic;
        }
        if(bLow <= bHigh + 2 * tolerance){
            break;
        }
        if ((iteration & 0x7ff) == 0) {
            printf("iteration: %d; gap: %f\n",iteration, bLow - bHigh);
        }
        cacheCall(cache,iHigh, &iHighCacheIndex, &iHighCompute);
        cacheCall(cache,iLow, &iLowCacheIndex, &iLowCompute);
        alpha1diff = labels[iHigh] * alpha1diff;
        alpha2diff = labels[iLow] * alpha2diff;
        if (currentHeuristic == 0){
            // Set the arguments to foph1
            //

            err = 0;
            err  = clSetKernelArg(foph1, 0, sizeof(cl_mem), &d_input_data);
            err  |= clSetKernelArg(foph1, 1, sizeof(cl_mem), &d_labels);
            err  |= clSetKernelArg(foph1, 2, sizeof(cl_mem), &d_trainingAlpha);
            err  |= clSetKernelArg(foph1, 3, sizeof(cl_mem), &d_F);
            err  |= clSetKernelArg(foph1, 4, sizeof(cl_mem), &d_lowFsFO);
            err  |= clSetKernelArg(foph1, 5, sizeof(cl_mem), &d_highFsFO);
            err  |= clSetKernelArg(foph1, 6, sizeof(cl_mem), &d_lowIndicesFO);
            err  |= clSetKernelArg(foph1, 7, sizeof(cl_mem), &d_highIndicesFO);
            err  |= clSetKernelArg(foph1, 8, sizeof(int), &nPoints);
            err  |= clSetKernelArg(foph1, 9, sizeof(int), &dFeatures);
            err  |= clSetKernelArg(foph1, 10, sizeof(float), &epsilon);
            err  |= clSetKernelArg(foph1, 11, sizeof(float), &Ce);
            err  |= clSetKernelArg(foph1, 12, sizeof(int), &iHigh);
            err  |= clSetKernelArg(foph1, 13, sizeof(int), &iLow);
            err  |= clSetKernelArg(foph1, 14, sizeof(float), &alpha1diff);
            err  |= clSetKernelArg(foph1, 15, sizeof(float), &alpha2diff);
            err  |= clSetKernelArg(foph1, 16, sizeof(float), &paramA);
            err  |= clSetKernelArg(foph1, 17, sizeof(float), &paramB);
            err  |= clSetKernelArg(foph1, 18, sizeof(float), &paramC);
            err  |= clSetKernelArg(foph1, 19, sizeof(int) * localFoph1Size, NULL);
            err  |= clSetKernelArg(foph1, 20, sizeof(float) * localFoph1Size, NULL);
            err  |= clSetKernelArg(foph1, 21, sizeof(cl_mem), &d_cache);
            err  |= clSetKernelArg(foph1, 22, sizeof(bool), &iHighCompute);
            err  |= clSetKernelArg(foph1, 23, sizeof(bool), &iLowCompute);
            err  |= clSetKernelArg(foph1, 24, sizeof(int), &iHighCacheIndex);
            err  |= clSetKernelArg(foph1, 25, sizeof(int),&iLowCacheIndex);

            if (err != CL_SUCCESS){
                printf("Error: Failed to set kernel arguments! %d\n", err);
                return err;
            }

            err = clEnqueueNDRangeKernel(queue, foph1, 1, NULL, &globalFoph1Size, &localFoph1Size, 0, NULL, NULL);
            if (err != CL_SUCCESS){
                printf("Error: Failed to execute kernel Foph1!\n");
                return err;
            }

            // Set the arguments to foph2
            //
            err = 0;
            err  = clSetKernelArg(foph2, 0, sizeof(cl_mem), &d_input_data);
            err  |= clSetKernelArg(foph2, 1, sizeof(cl_mem), &d_labels);
            err  |= clSetKernelArg(foph2, 2, sizeof(cl_mem), &d_trainingAlpha);
            err  |= clSetKernelArg(foph2, 3, sizeof(cl_mem), &d_kernelDiag);
            err  |= clSetKernelArg(foph2, 4, sizeof(cl_mem), &d_lowFsFO);
            err  |= clSetKernelArg(foph2, 5, sizeof(cl_mem), &d_highFsFO);
            err  |= clSetKernelArg(foph2, 6, sizeof(cl_mem), &d_lowIndicesFO);
            err  |= clSetKernelArg(foph2, 7, sizeof(cl_mem), &d_highIndicesFO);
            err  |= clSetKernelArg(foph2, 8, sizeof(cl_mem), &d_results);
            err  |= clSetKernelArg(foph2, 9, sizeof(float), &cost);
            err  |= clSetKernelArg(foph2, 10, sizeof(int), &dFeatures);
            err  |= clSetKernelArg(foph2, 11, sizeof(int), &numGroups_foph1);
            err  |= clSetKernelArg(foph2, 12, sizeof(float), &paramA);
            err  |= clSetKernelArg(foph2, 13, sizeof(float), &paramB);
            err  |= clSetKernelArg(foph2, 14, sizeof(float), &paramC);
            err  |= clSetKernelArg(foph2, 15, sizeof(int) * localFoph2Size, NULL);
            err  |= clSetKernelArg(foph2, 16, sizeof(float) * localFoph2Size, NULL);

            if (err != CL_SUCCESS){
                printf("Error: Failed to set kernel arguments! %d\n", err);
                return err;
            }

            err = clEnqueueNDRangeKernel(queue, foph2, 1, NULL, &globalFoph2Size, &localFoph2Size, 0, NULL, NULL);
            if (err != CL_SUCCESS){
                printf("Error: Failed to execute kernel Foph2!\n");
                return err;
            }

            // Wait for the command queue to get serviced before reading back results
            //
            err = clFinish(queue);

            if (err != CL_SUCCESS){
                printf("Error: waiting for queue to finish failed\n");
                return err;
            }

            getResults(queue, d_results, results, &bLow, &bHigh, &iLow, &iHigh, &alpha1diff, &alpha2diff);

        }else{
            // Set the arguments to soph1
            //

//            clock_t startSoph1 = clock();
            err = 0;
            err  = clSetKernelArg(soph1, 0, sizeof(cl_mem), &d_input_data);
            err  |= clSetKernelArg(soph1, 1, sizeof(cl_mem), &d_labels);
            err  |= clSetKernelArg(soph1, 2, sizeof(cl_mem), &d_trainingAlpha);
            err  |= clSetKernelArg(soph1, 3, sizeof(cl_mem), &d_F);
            err  |= clSetKernelArg(soph1, 4, sizeof(cl_mem), &d_highFsSO1);
            err  |= clSetKernelArg(soph1, 5, sizeof(cl_mem), &d_highIndicesSO1);
            err  |= clSetKernelArg(soph1, 6, sizeof(int), &nPoints);
            err  |= clSetKernelArg(soph1, 7, sizeof(int), &dFeatures);
            err  |= clSetKernelArg(soph1, 8, sizeof(float), &epsilon);
            err  |= clSetKernelArg(soph1, 9, sizeof(float), &Ce);
            err  |= clSetKernelArg(soph1, 10, sizeof(int), &iHigh);
            err  |= clSetKernelArg(soph1, 11, sizeof(int), &iLow);
            err  |= clSetKernelArg(soph1, 12, sizeof(float), &alpha1diff);
            err  |= clSetKernelArg(soph1, 13, sizeof(float), &alpha2diff);
            err  |= clSetKernelArg(soph1, 14, sizeof(float), &paramA);
            err  |= clSetKernelArg(soph1, 15, sizeof(float), &paramB);
            err  |= clSetKernelArg(soph1, 16, sizeof(float), &paramC);
            err  |= clSetKernelArg(soph1, 17, sizeof(int) * localSoph1Size, NULL);
            err  |= clSetKernelArg(soph1, 18, sizeof(float) * localSoph1Size, NULL);
            err  |= clSetKernelArg(soph1, 19, sizeof(cl_mem), &d_cache);
            err  |= clSetKernelArg(soph1, 20, sizeof(bool), &iHighCompute);
            err  |= clSetKernelArg(soph1, 21, sizeof(bool), &iLowCompute);
            err  |= clSetKernelArg(soph1, 22, sizeof(int), &iHighCacheIndex);
            err  |= clSetKernelArg(soph1, 23, sizeof(int),&iLowCacheIndex);

            if (err != CL_SUCCESS){
                printf("Error: Failed to set kernel arguments! %d\n", err);
                return err;
            }

            err = clEnqueueNDRangeKernel(queue, soph1, 1, NULL, &globalSoph1Size, &localSoph1Size, 0, NULL, NULL);
            if (err != CL_SUCCESS){
                printf("Error: Failed to execute kernel Soph1!\n");
                return err;
            }
//            clFinish(queue);
//            diffSoph1 = clock() - startSoph1;

//            clock_t startSoph2 = clock();

            // Set the arguments to soph2
            //
            err = 0;
            err  |= clSetKernelArg(soph2, 0, sizeof(cl_mem), &d_highFsSO1);
            err  |= clSetKernelArg(soph2, 1, sizeof(cl_mem), &d_highIndicesSO1);
            err  |= clSetKernelArg(soph2, 2, sizeof(cl_mem), &d_results);
            err  |= clSetKernelArg(soph2, 3, sizeof(int), &numGroups_soph1);
            err  |= clSetKernelArg(soph2, 4, sizeof(int) * localSoph2Size, NULL);
            err  |= clSetKernelArg(soph2, 5, sizeof(float) * localSoph2Size, NULL);

            if (err != CL_SUCCESS){
                printf("Error: Failed to set kernel arguments! %d\n", err);
                return err;
            }

            err = clEnqueueNDRangeKernel(queue, soph2, 1, NULL, &globalSoph2Size, &localSoph2Size, 0, NULL, NULL);
            if (err != CL_SUCCESS){
                printf("Error: Failed to execute kernel Soph2!\n");
                return err;
            }
//            clFinish(queue);
//            diffSoph2 = clock() - startSoph2;

            // Set the arguments to soph3
            //
            clFinish(queue);
            getResults(queue, d_results, results, &bLow, &bHigh, &iLow, &iHigh, &alpha1diff, &alpha2diff);
//            clock_t startSoph3 = clock();
            cacheCall(cache, iHigh, &iHighCacheIndex, &iHighCompute);

            err = 0;
            err  = clSetKernelArg(soph3, 0, sizeof(cl_mem), &d_input_data);
            err  |= clSetKernelArg(soph3, 1, sizeof(cl_mem), &d_labels);
            err  |= clSetKernelArg(soph3, 2, sizeof(cl_mem), &d_trainingAlpha);
            err  |= clSetKernelArg(soph3, 3, sizeof(cl_mem), &d_F);
            err  |= clSetKernelArg(soph3, 4, sizeof(cl_mem), &d_kernelDiag);
            err  |= clSetKernelArg(soph3, 5, sizeof(cl_mem), &d_lowFsSO3);
            err  |= clSetKernelArg(soph3, 6, sizeof(cl_mem), &d_lowIndicesSO3);
            err  |= clSetKernelArg(soph3, 7, sizeof(cl_mem), &d_deltaFsSO3);
            err  |= clSetKernelArg(soph3, 8, sizeof(cl_mem), &d_results);
            err  |= clSetKernelArg(soph3, 9, sizeof(int), &iHigh);
            err  |= clSetKernelArg(soph3, 10, sizeof(float), &bHigh);
            err  |= clSetKernelArg(soph3, 11, sizeof(int), &nPoints);
            err  |= clSetKernelArg(soph3, 12, sizeof(int), &dFeatures);
            err  |= clSetKernelArg(soph3, 13, sizeof(float), &epsilon);
            err  |= clSetKernelArg(soph3, 14, sizeof(float), &Ce);
            err  |= clSetKernelArg(soph3, 15, sizeof(float), &paramA);
            err  |= clSetKernelArg(soph3, 16, sizeof(float), &paramB);
            err  |= clSetKernelArg(soph3, 17, sizeof(float), &paramC);
            err  |= clSetKernelArg(soph3, 18, sizeof(int) * localSoph3Size, NULL);
            err  |= clSetKernelArg(soph3, 19, sizeof(float) * localSoph3Size, NULL);
            err  |= clSetKernelArg(soph3, 20, sizeof(cl_mem), &d_cache);
            err  |= clSetKernelArg(soph3, 21, sizeof(bool), &iHighCompute);
            err  |= clSetKernelArg(soph3, 22, sizeof(int), &iHighCacheIndex);



            if (err != CL_SUCCESS){
                printf("Error: Failed to set kernel arguments! %d\n", err);
                return err;
            }

            err = clEnqueueNDRangeKernel(queue, soph3, 1, NULL, &globalSoph3Size, &localSoph3Size, 0, NULL, NULL);
            if (err != CL_SUCCESS){
                printf("Error: Failed to execute kernel Soph3!\n");
                return err;
            }
//            clFinish(queue);
//            diffSoph3 = clock() - startSoph3;

            // Set the arguments to soph4
            //
//            clock_t startSoph4 = clock();

            err = 0;
            err  = clSetKernelArg(soph4, 0, sizeof(cl_mem), &d_input_data);
            err  |= clSetKernelArg(soph4, 1, sizeof(cl_mem), &d_labels);
            err  |= clSetKernelArg(soph4, 2, sizeof(cl_mem), &d_trainingAlpha);
            err  |= clSetKernelArg(soph4, 3, sizeof(cl_mem), &d_kernelDiag);
            err  |= clSetKernelArg(soph4, 4, sizeof(cl_mem), &d_lowFsSO3);
            err  |= clSetKernelArg(soph4, 5, sizeof(cl_mem), &d_lowIndicesSO3);
            err  |= clSetKernelArg(soph4, 6, sizeof(cl_mem), &d_deltaFsSO3);
            err  |= clSetKernelArg(soph4, 7, sizeof(cl_mem), &d_results);
            err  |= clSetKernelArg(soph4, 8, sizeof(float), &cost);
            err  |= clSetKernelArg(soph4, 9, sizeof(int), &dFeatures);
            err  |= clSetKernelArg(soph4, 10, sizeof(int), &numGroups_soph3);
            err  |= clSetKernelArg(soph4, 11, sizeof(float), &paramA);
            err  |= clSetKernelArg(soph4, 12, sizeof(float), &paramB);
            err  |= clSetKernelArg(soph4, 13, sizeof(float), &paramC);
            err  |= clSetKernelArg(soph4, 14, sizeof(cl_mem), &d_F);
            err  |= clSetKernelArg(soph4, 15, sizeof(int) * localSoph4Size, NULL);
            err  |= clSetKernelArg(soph4, 16, sizeof(float) * localSoph4Size, NULL);

            if (err != CL_SUCCESS){
                printf("Error: Failed to set kernel arguments! %d\n", err);
                return err;
            }

            err = clEnqueueNDRangeKernel(queue, soph4, 1, NULL, &globalSoph4Size, &localSoph4Size, 0, NULL, NULL);
            if (err != CL_SUCCESS){
                printf("Error: Failed to execute kernel Soph4!\n");
                return err;
            }
//            clFinish(queue);
//            diffSoph4 = clock() - startSoph4;

            // Wait for the command queue to get serviced before reading back results
            //
            err = clFinish(queue);

            if (err != CL_SUCCESS){
                printf("Error: waiting for queue to finish failed\n");
                return err;
            }
//            clock_t startGetResults = clock();

            getResults(queue, d_results, results, &bLow, &bHigh, &iLow, &iHigh, &alpha1diff, &alpha2diff);
//            clFinish(queue);
//            diffGetResults = clock() - startGetResults;
            //printf("iLow:%d, iHigh:%d\n", (int)results[2], (int)results[3]);
        }
        if(heuristic == 2){
            addIteration(progress, bLow - bHigh);
        }
    }
    printf("INFO: %d iterations\n", iteration);
    printf("INFO: bLow: %f, bHigh %f\n", bLow, bHigh);
//    diffTot = clock() - startTot;
//    float msecTot = diffTot * 1000.0 / CLOCKS_PER_SEC;
//    printf("Total time taken %.5f milliseconds\n", msecTot);
//    float msecSoph1 = diffSoph1 * 1000.0 / CLOCKS_PER_SEC;
//    printf("Soph1 time taken %.5f milliseconds\n", msecSoph1);
//    float msecSoph2 = diffSoph2 * 1000.0 / CLOCKS_PER_SEC;
//    printf("Soph2 time taken %.5f milliseconds\n", msecSoph2);
//    float msecSoph3 = diffSoph3 * 1000.0 / CLOCKS_PER_SEC;
//    printf("Soph3 time taken %.5f milliseconds\n", msecSoph3);
//    float msecSoph4 = diffSoph4 * 1000.0 / CLOCKS_PER_SEC;
//    printf("Soph4 time taken %.5f milliseconds\n", msecSoph4);
//    float msecGetResults = diffGetResults * 1000.0 / CLOCKS_PER_SEC;
//    printf("GetResult time taken %.5f milliseconds\n", msecGetResults);
    // get training alpha
    float *trainingAlpha = (float *)malloc(sizeof(float) * nPoints);

    err = clEnqueueReadBuffer(queue, d_trainingAlpha, CL_TRUE, 0, sizeof(float) * nPoints, trainingAlpha, 0, NULL, NULL);
    if (err != CL_SUCCESS){
        printf("Error: Failed to read buffer\n");
        return err;
    }
    // save results
    *p_rho = (bHigh + bLow)/2;
    int nSV = 0;
    for(int i = 0; i < nPoints; i++){
        if (trainingAlpha[i] > epsilon){
            nSV++;
        }
    }
    int index = 0;
    *p_nSV = nSV;
    *p_supportVectors = (float *)malloc(sizeof(float) * nSV * dFeatures);
    *p_signedAlpha = (float *)malloc(sizeof(float) * nSV);
    for(int i = 0; i < nPoints; i++){
        if(trainingAlpha[i] > epsilon){
            (* p_signedAlpha)[index] = labels[i] * trainingAlpha[i];
            for(int j = 0; j < dFeatures; j++){
                (* p_supportVectors)[index*dFeatures + j] = input_data[i * dFeatures + j];
            }
            index ++;
        }
    }
    // Shutdown and cleanup
    //
    printf("%d Cache Hits!\n",cache->hits);
    printf("%d Cache Misses!\n",cache->misses);
    return 0;
}