Caffe源码中各种依赖库的作用及简单使用

前端之家收集整理的这篇文章主要介绍了Caffe源码中各种依赖库的作用及简单使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.Boost库:它是一个可移植、跨平台,提供源代码的C++库,作为标准库的后备。

在Caffe中用到的Boost头文件包括

(1)、shared_ptr.hpp:智能指针,使用它可以不需要考虑内存释放的问题;

(2)、date_time/posix_time/posix_time.hpp:时间操作函数

(3)、python.hpp:C++/Python互操作;

(4)、make_shared.hpp:make_shared工厂函数代替new操作符;

(5)、python/raw_function.hpp:C++/Python互操作;

(6)、python/suite/indexing/vector_indexing_suite.hpp:C++/Python互操作;

(7)、thread.hpp:线程操作;

(8)、math/special_functions/next.hpp:数学函数

2.GFlags库:它是google的一个开源的处理命令行参数的库,使用C++开发,可以替代getopt函数。GFlags与getopt函数不同,在GFlags中,标记的定义分散在源代码中,不需要列举在一个地方。

3.GLog库:它是一个应用程序的日志库,提供基于C++风格的流的日志API,以及各种辅助的宏。它的使用方式与C++的stream操作类似。

4.LevelDB库:它是google实现的一个非常高效的Key-Value数据库。它是单进程的服务,性能非常高。它只是一个C/C++编程语言的库,不包含网络服务封装。

LevelDB特点:(1)、LevelDB是一个持久化存储的KV系统,它将大部分数据存储到磁盘上;(2)、LevelDB在存储数据时,是根据记录的Key值有序存储的;(3)、像大多数KV系统一样,LevelDB的操作接口很简单,基本操作包括写记录,读记录以及删除记录,也支持针对多条操作的原子批量操作;(4)、LevelDB支持数据快照(snapshot)功能,使得读取操作不受写操作影响,可以在读操作过程中始终看到一致的数据;(5)、LevelDB支持数据压缩(Snappy)等操作。

5.LMDB库:它是一个超级快、超级小的Key-Value数据存储服务,是由OpenLDAP项目的Symas开发的。使用内存映射文件,因此读取的性能跟内存数据库一样,其大小受限于虚拟地址空间的大小。

6.ProtoBuf库:GoogleProtocol Buffer(简称ProtoBuf),它是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或RPC数据交换格式。可用于通信协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。

要使用ProtoBuf库,首先需要自己编写一个.proto文件,定义我们程序中需要处理的结构化数据,在protobuf中,结构化数据被称为Message。在一个.proto文件中可以定义多个消息类型。用Protobuf编译器(protoc.exe)将.proto文件编译成目标语言,会生成对应的.h文件和.cc文件,.proto文件中的每一个消息有一个对应的类。

7.HDF5库:HDF(HierarchicalData File)是美国国家高级计算应用中心(NCSA)为了满足各种领域研究需求而研制的一种能高效存储和分发科学数据的新型数据格式。它可以存储不同类型的图像和数码数据的文件格式,并且可以在不同类型的机器上传输,同时还有统一处理这种文件格式的函数库。HDF5推出于1998年,相较于以前的HDF文件,可以说是一种全新的文件格式。HDF5是用于存储科学数据的一种文件格式和库文件

HDF5是分层式数据管理结构。HDF5不但能处理更多的对象,存储更大的文件支持并行I/O,线程和具备现代操作系统与应用程序所要求的其它特性,而且数据模型变得更简单,概括性更强。

HDF5只有两种基本结构,组(group)和数据集(dataset)。组,包含0个或多个HDF5对象以及支持元数据(Metadata)的一个群组结构。数据集,数据元素的一个多维数组以及支持元数据。

8.snappy库:它是一个C++库,用来压缩和解压缩的开发包。它旨在提供高速压缩速度和合理的压缩率。Snappy比zlib更快,但文件相对要大20%到100%。

下面为各个库的简单使用举例:

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <assert.h>
  5. #include <fstream>
  6.  
  7. #include <boost/shared_ptr.hpp>
  8. #include <boost/array.hpp>
  9. #include <boost/thread.hpp>
  10. #include <boost/bind.hpp>
  11. #include <gflags/gflags.h>
  12. #include <glog/logging.h>
  13. #include <levelDB/db.h>
  14. #include <lmdb.h>
  15. #include <hdf5.h>
  16. #include <snappy-c.h>
  17.  
  18. #include <windows.h>
  19.  
  20. #include "ml.helloworld.pb.h"
  21.  
  22. int test_Boost();
  23. int test_GFlags(int argc,char* argv[]);
  24. int test_GLog();
  25. int test_LevelDB();
  26. int test_LMDB();
  27. int test_ProtoBuf();
  28. int test_HDF5();
  29. int test_Snappy();
  30.  
  31.  
  32. int main(int argc,char* argv[])
  33. {
  34. //std::cout << argv[0] << std::endl;//E:\GitCode\Caffe\lib\dbg\x86_vc12\testThridLibrary[dbg_x86_vc12].exe
  35. //test_Boost();
  36. //test_GFlags(argc,argv);
  37. //test_GLog();
  38. //test_LevelDB();
  39. //test_LMDB();
  40. //test_ProtoBuf();
  41. //test_HDF5();
  42. test_Snappy();
  43.  
  44. std::cout << "ok!!!" << std::endl;
  45. return 0;
  46. }
  47.  
  48. class implementation {
  49. public:
  50. ~implementation() { std::cout << "destroying implementation\n"; }
  51. void do_something() { std::cout << "did something\n"; }
  52. };
  53.  
  54. int test_Boost()
  55. {
  56. //http://www.cnblogs.com/tianfang/archive/2008/09/19/1294521.html
  57. boost::shared_ptr<implementation> sp1(new implementation());
  58. std::cout << "The Sample now has " << sp1.use_count() << " references\n";
  59.  
  60. boost::shared_ptr<implementation> sp2 = sp1;
  61. std::cout << "The Sample now has " << sp2.use_count() << " references\n";
  62.  
  63. sp1.reset();
  64. std::cout << "After Reset sp1. The Sample now has " << sp2.use_count() << " references\n";
  65.  
  66. sp2.reset();
  67. std::cout << "After Reset sp2.\n";
  68.  
  69. return 0;
  70. }
  71.  
  72. DEFINE_bool(big_menu,true,"Include 'advanced' options in the menu listing");
  73. DEFINE_string(languages,"english,french,german","comma-separated list of languages to offer in the 'lang' menu");
  74.  
  75. int test_GFlags(int argc,char* argv[])
  76. {
  77. //http://dreamrunner.org/blog/2014/03/09/gflags-jian-ming-shi-yong/
  78. //http://www.leoox.com/?p=270
  79. int tmp_argc = 3;
  80. char** tmp_argv = NULL;
  81. tmp_argv = new char*[3];
  82. tmp_argv[0] = "";
  83. tmp_argv[1] = "--big_menu=false";
  84. tmp_argv[2] = "--languages=chinese";
  85.  
  86. //google::ParseCommandLineFlags(&argc,&argv,true);
  87. google::ParseCommandLineFlags(&tmp_argc,&tmp_argv,true);
  88.  
  89. std::cout << "argc=" << argc << std::endl;
  90. if (FLAGS_big_menu) {
  91. std::cout << "big menu is ture" << std::endl;
  92. }
  93. else {
  94. std::cout << "big menu is flase" << std::endl;
  95. }
  96.  
  97. std::cout << "languages=" << FLAGS_languages << std::endl;
  98.  
  99. return 0;
  100. }
  101.  
  102. void thread1_test()
  103. {
  104. std::string strTmp = "thread1_test";
  105. for (int i = 0; i< 1000; i++) {
  106. //LOG(INFO) << i;
  107. LOG_IF(INFO,i < 10) << i;
  108. //CHECK_EQ(i,100) << "error!";
  109. //LOG(INFO) << strTmp;
  110. //Sleep(10);
  111. }
  112. }
  113.  
  114. void thread2_test()
  115. {
  116. std::string strTmp = "thread2_test";
  117. for (int i = 1000; i< 2000; i++) {
  118. //LOG(INFO) << i;
  119. LOG_IF(INFO,i < 1100) << i;
  120. //LOG(INFO) << strTmp;
  121. //Sleep(10);
  122. }
  123. }
  124.  
  125. int test_GLog()
  126. {
  127. //http://www.yeolar.com/note/2014/12/20/glog/
  128. //http://www.cppblog.com/pizzx/archive/2014/06/18/207320.aspx
  129. const char* exe = "E:/GitCode/Caffe/lib/dbg/x86_vc12/testThridLibrary[dbg_x86_vc12].exe";
  130. //Initialize Google's logging library.
  131. //google::InitGoogleLogging(argv[0]);
  132. google::InitGoogleLogging(exe);
  133.  
  134. //为不同级别的日志设置不同的文件basename。
  135. google::SetLogDestination(google::INFO,"E:/tmp/loginfo");
  136. google::SetLogDestination(google::WARNING,"E:/tmp/logwarn");
  137. google::SetLogDestination(google::GLOG_ERROR,"E:/tmp/logerror");
  138. //缓存的最大时长,超时会写入文件
  139. FLAGS_logbufsecs = 60;
  140.  
  141. //单个日志文件最大,单位M
  142. FLAGS_max_log_size = 10;
  143.  
  144. //设置为true,就不会写日志文件
  145. FLAGS_logtostderr = false;
  146. boost::thread t1(boost::bind(&thread1_test));
  147. boost::thread t2(boost::bind(&thread2_test));
  148.  
  149. t1.join();
  150. t2.join();
  151.  
  152. //LOG(FATAL)<<"exit";
  153. google::ShutdownGoogleLogging();
  154.  
  155. return 0;
  156. }
  157.  
  158. int test_LevelDB()
  159. {
  160. //http://www.cnblogs.com/haippy/archive/2011/12/04/2276064.html
  161. //http://www.bubuko.com/infodetail-411090.html
  162. //http://qiuqiang1985.iteye.com/blog/1255365
  163. leveldb::DB* db;
  164. leveldb::Options options;
  165. options.create_if_missing = true;
  166. leveldb::Status status = leveldb::DB::Open(options,"E:/tmp/testLevelDB",&db);
  167. assert(status.ok());
  168.  
  169. //write key1,value1
  170. std::string key = "key";
  171. std::string value = "value";
  172.  
  173. //write
  174. status = db->Put(leveldb::WriteOptions(),key,value);
  175. assert(status.ok());
  176.  
  177. //read
  178. status = db->Get(leveldb::ReadOptions(),&value);
  179. assert(status.ok());
  180. std::cout << value << std::endl;
  181. std::string key2 = "key2";
  182.  
  183. //move the value under key to key2
  184. status = db->Put(leveldb::WriteOptions(),key2,value);
  185. assert(status.ok());
  186. //delete
  187. status = db->Delete(leveldb::WriteOptions(),key);
  188. assert(status.ok());
  189.  
  190. status = db->Get(leveldb::ReadOptions(),&value);
  191. assert(status.ok());
  192. std::cout << key2 << "===" << value << std::endl;
  193. status = db->Get(leveldb::ReadOptions(),&value);
  194. if (!status.ok())
  195. std::cerr << key << " " << status.ToString() << std::endl;
  196. else
  197. std::cout << key << "===" << value << std::endl;
  198.  
  199. delete db;
  200.  
  201. return 0;
  202. }
  203.  
  204. #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS,#expr)
  205. #define RES(err,expr) ((rc = expr) == (err) || (CHECK(!rc,#expr),0))
  206. #define CHECK(test,msg) ((test) ? (void)0 : ((void)fprintf(stderr,\
  207. "%s:%d: %s: %s\n",__FILE__,__LINE__,msg,mdb_strerror(rc)),abort()))
  208.  
  209. int test_LMDB()
  210. {
  211. //http://www.jianshu.com/p/yzFf8j
  212. //./lmdb-mdb.master/libraries/liblmdb/mtest.c
  213. int i = 0,j = 0,rc;
  214. MDB_env *env;
  215. MDB_dbi dbi;
  216. MDB_val key,data;
  217. MDB_txn *txn;
  218. MDB_stat mst;
  219. MDB_cursor *cursor,*cur2;
  220. MDB_cursor_op op;
  221. int count;
  222. int *values;
  223. char sval[32] = "";
  224.  
  225. srand(time(NULL));
  226.  
  227. count = (rand() % 384) + 64;
  228. values = (int *)malloc(count*sizeof(int));
  229.  
  230. for (i = 0; i<count; i++) {
  231. values[i] = rand() % 1024;
  232. }
  233.  
  234. E(mdb_env_create(&env));
  235. E(mdb_env_set_maxreaders(env,1));
  236. E(mdb_env_set_mapsize(env,10485760));
  237. //E(mdb_env_open(env,"./testdb",MDB_FIXEDMAP /*|MDB_NOSYNC*/,0664));
  238. E(mdb_env_open(env,"E:/tmp/testLMDB",0664));
  239.  
  240. E(mdb_txn_begin(env,NULL,&txn));
  241. E(mdb_dbi_open(txn,&dbi));
  242.  
  243. key.mv_size = sizeof(int);
  244. key.mv_data = sval;
  245.  
  246. printf("Adding %d values\n",count);
  247. for (i = 0; i<count; i++) {
  248. sprintf(sval,"%03x %d foo bar",values[i],values[i]);
  249. /* Set <data> in each iteration,since MDB_NOOVERWRITE may modify it */
  250. data.mv_size = sizeof(sval);
  251. data.mv_data = sval;
  252. if (RES(MDB_KEYEXIST,mdb_put(txn,dbi,&key,&data,MDB_NOOVERWRITE))) {
  253. j++;
  254. data.mv_size = sizeof(sval);
  255. data.mv_data = sval;
  256. }
  257. }
  258. if (j) printf("%d duplicates skipped\n",j);
  259. E(mdb_txn_commit(txn));
  260. E(mdb_env_stat(env,&mst));
  261.  
  262. E(mdb_txn_begin(env,MDB_RDONLY,&txn));
  263. E(mdb_cursor_open(txn,&cursor));
  264. while ((rc = mdb_cursor_get(cursor,MDB_NEXT)) == 0) {
  265. printf("key: %p %.*s,data: %p %.*s\n",key.mv_data,(int)key.mv_size,(char *)key.mv_data,data.mv_data,(int)data.mv_size,(char *)data.mv_data);
  266. }
  267. CHECK(rc == MDB_NOTFOUND,"mdb_cursor_get");
  268. mdb_cursor_close(cursor);
  269. mdb_txn_abort(txn);
  270.  
  271. j = 0;
  272. key.mv_data = sval;
  273. for (i = count - 1; i > -1; i -= (rand() % 5)) {
  274. j++;
  275. txn = NULL;
  276. E(mdb_txn_begin(env,&txn));
  277. sprintf(sval,"%03x ",values[i]);
  278. if (RES(MDB_NOTFOUND,mdb_del(txn,NULL))) {
  279. j--;
  280. mdb_txn_abort(txn);
  281. }
  282. else {
  283. E(mdb_txn_commit(txn));
  284. }
  285. }
  286. free(values);
  287. printf("Deleted %d values\n",j);
  288.  
  289. E(mdb_env_stat(env,&mst));
  290. E(mdb_txn_begin(env,&cursor));
  291. printf("Cursor next\n");
  292. while ((rc = mdb_cursor_get(cursor,MDB_NEXT)) == 0) {
  293. printf("key: %.*s,data: %.*s\n","mdb_cursor_get");
  294. printf("Cursor last\n");
  295. E(mdb_cursor_get(cursor,MDB_LAST));
  296. printf("key: %.*s,(char *)data.mv_data);
  297. printf("Cursor prev\n");
  298. while ((rc = mdb_cursor_get(cursor,MDB_PREV)) == 0) {
  299. printf("key: %.*s,"mdb_cursor_get");
  300. printf("Cursor last/prev\n");
  301. E(mdb_cursor_get(cursor,(char *)data.mv_data);
  302. E(mdb_cursor_get(cursor,MDB_PREV));
  303. printf("key: %.*s,(char *)data.mv_data);
  304.  
  305. mdb_cursor_close(cursor);
  306. mdb_txn_abort(txn);
  307.  
  308. printf("Deleting with cursor\n");
  309. E(mdb_txn_begin(env,&cur2));
  310. for (i = 0; i<50; i++) {
  311. if (RES(MDB_NOTFOUND,mdb_cursor_get(cur2,MDB_NEXT)))
  312. break;
  313. printf("key: %p %.*s,(char *)data.mv_data);
  314. E(mdb_del(txn,NULL));
  315. }
  316.  
  317. printf("Restarting cursor in txn\n");
  318. for (op = MDB_FIRST,i = 0; i <= 32; op = MDB_NEXT,i++) {
  319. if (RES(MDB_NOTFOUND,op)))
  320. break;
  321. printf("key: %p %.*s,(char *)data.mv_data);
  322. }
  323. mdb_cursor_close(cur2);
  324. E(mdb_txn_commit(txn));
  325.  
  326. printf("Restarting cursor outside txn\n");
  327. E(mdb_txn_begin(env,&cursor));
  328. for (op = MDB_FIRST,mdb_cursor_get(cursor,(char *)data.mv_data);
  329. }
  330. mdb_cursor_close(cursor);
  331. mdb_txn_abort(txn);
  332.  
  333. mdb_dbi_close(env,dbi);
  334. mdb_env_close(env);
  335.  
  336. return 0;
  337. }
  338.  
  339. void ListMsg(const lm::helloworld& msg) {
  340. std::cout << msg.id() << std::endl;
  341. std::cout << msg.str() << std::endl;
  342. }
  343.  
  344. int test_ProtoBuf()
  345. {
  346. //http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/
  347. //http://blog.163.com/jiang_tao_2010/blog/static/12112689020114305013458/
  348. //http://www.cnblogs.com/dkblog/archive/2012/03/27/2419010.html
  349. // 1-->首先编写一个ml.helloworld.proto文件内容如下:
  350. /*
  351. Syntax = "proto2";
  352. package lm;
  353.  
  354. message helloworld
  355. {
  356. required int32 id = 1; // ID
  357. required string str = 2; // str
  358. optional int32 opt = 3; //optional field
  359. }
  360. */
  361.  
  362. // 2-->利用protoc.exe生成ml.helloworld.pb.h和ml.hellowrold.ph.cc
  363.  
  364. // 3-->Writer,将把一个结构化数据写入磁盘,以便其他人来读取
  365. /*lm::helloworld msg1;
  366. msg1.set_id(101);
  367. msg1.set_str("hello");
  368.  
  369. // Write the new address book back to disk.
  370. std::fstream output("./log",std::ios::out | std::ios::trunc | std::ios::binary);
  371. if (!msg1.SerializeToOstream(&output)) {
  372. std::cerr << "Failed to write msg." << std::endl;
  373. return -1;
  374. }*/
  375.  
  376. // 4-->Reader,读取结构化数据,log文件
  377. lm::helloworld msg2;
  378. std::fstream input("./log",std::ios::in | std::ios::binary);
  379. if (!msg2.ParseFromIstream(&input)) {
  380. std::cerr << "Failed to parse address book." << std::endl;
  381. return -1;
  382. }
  383.  
  384. ListMsg(msg2);
  385.  
  386. return 0;
  387. }
  388.  
  389. #define H5FILE_NAME "E:/tmp/HDF5/SDS.h5"
  390. #define DATASETNAME "IntArray"
  391. #define NX 5 /* dataset dimensions */
  392. #define NY 6
  393. #define RANK 2
  394.  
  395. int test_HDF5_write_HDF5_Data()
  396. {
  397. hid_t file,dataset; /* file and dataset handles */
  398. hid_t datatype,dataspace; /* handles */
  399. hsize_t dimsf[2]; /* dataset dimensions */
  400. herr_t status;
  401. int data[NX][NY]; /* data to write */
  402. int i,j;
  403.  
  404. //Data and output buffer initialization.
  405. for (j = 0; j < NX; j++)
  406. for (i = 0; i < NY; i++)
  407. data[j][i] = i + j + 100;//changed
  408. /*
  409. * 0 1 2 3 4 5
  410. * 1 2 3 4 5 6
  411. * 2 3 4 5 6 7
  412. * 3 4 5 6 7 8
  413. * 4 5 6 7 8 9
  414. */
  415.  
  416. /*
  417. * Create a new file using H5F_ACC_TRUNC access,* default file creation properties,and default file
  418. * access properties.
  419. */
  420. file = H5Fcreate(H5FILE_NAME,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
  421.  
  422. /*
  423. * Describe the size of the array and create the data space for fixed
  424. * size dataset.
  425. */
  426. dimsf[0] = NX;
  427. dimsf[1] = NY;
  428. dataspace = H5Screate_simple(RANK,dimsf,NULL);
  429.  
  430. /*
  431. * Define datatype for the data in the file.
  432. * We will store little endian INT numbers.
  433. */
  434. datatype = H5Tcopy(H5T_NATIVE_INT);
  435. status = H5Tset_order(datatype,H5T_ORDER_LE);
  436.  
  437. /*
  438. * Create a new dataset within the file using defined dataspace and
  439. * datatype and default dataset creation properties.
  440. */
  441. dataset = H5Dcreate2(file,DATASETNAME,datatype,dataspace,H5P_DEFAULT);
  442.  
  443. //Write the data to the dataset using default transfer properties.
  444. status = H5Dwrite(dataset,H5T_NATIVE_INT,H5S_ALL,data);
  445.  
  446. //Close/release resources.
  447. H5Sclose(dataspace);
  448. H5Tclose(datatype);
  449. H5Dclose(dataset);
  450. H5Fclose(file);
  451.  
  452. return 0;
  453. }
  454.  
  455. #define NX_SUB 3 /* hyperslab dimensions */
  456. #define NY_SUB 4
  457. #define NX 7 /* output buffer dimensions */
  458. #define NY 7
  459. #define NZ 3
  460. #define RANK 2
  461. #define RANK_OUT 3
  462.  
  463. int test_HDF5_read_HDF5_data()
  464. {
  465. hid_t file,dataset; /* handles */
  466. hid_t datatype,dataspace;
  467. hid_t memspace;
  468. H5T_class_t t_class; /* data type class */
  469. H5T_order_t order; /* data order */
  470. size_t size; /*
  471. * size of the data element
  472. * stored in file
  473. */
  474. hsize_t dimsm[3]; /* memory space dimensions */
  475. hsize_t dims_out[2]; /* dataset dimensions */
  476. herr_t status;
  477.  
  478. int data_out[NX][NY][NZ]; /* output buffer */
  479.  
  480. hsize_t count[2]; /* size of the hyperslab in the file */
  481. hsize_t offset[2]; /* hyperslab offset in the file */
  482. hsize_t count_out[3]; /* size of the hyperslab in memory */
  483. hsize_t offset_out[3]; /* hyperslab offset in memory */
  484. int i,j,k,status_n,rank;
  485.  
  486. for (j = 0; j < NX; j++) {
  487. for (i = 0; i < NY; i++) {
  488. for (k = 0; k < NZ; k++)
  489. data_out[j][i][k] = 0 - 1000;//changed
  490. }
  491. }
  492.  
  493. /*
  494. * Open the file and the dataset.
  495. */
  496. file = H5Fopen(H5FILE_NAME,H5F_ACC_RDONLY,H5P_DEFAULT);
  497. dataset = H5Dopen2(file,H5P_DEFAULT);
  498.  
  499. /*
  500. * Get datatype and dataspace handles and then query
  501. * dataset class,order,size,rank and dimensions.
  502. */
  503. datatype = H5Dget_type(dataset); /* datatype handle */
  504. t_class = H5Tget_class(datatype);
  505. if (t_class == H5T_INTEGER) printf("Data set has INTEGER type \n");
  506. order = H5Tget_order(datatype);
  507. if (order == H5T_ORDER_LE) printf("Little endian order \n");
  508.  
  509. size = H5Tget_size(datatype);
  510. printf("Data size is %d \n",(int)size);
  511.  
  512. dataspace = H5Dget_space(dataset); /* dataspace handle */
  513. rank = H5Sget_simple_extent_ndims(dataspace);
  514. status_n = H5Sget_simple_extent_dims(dataspace,dims_out,NULL);
  515. printf("rank %d,dimensions %lu x %lu \n",rank,(unsigned long)(dims_out[0]),(unsigned long)(dims_out[1]));
  516.  
  517. /*
  518. * Define hyperslab in the dataset.
  519. */
  520. offset[0] = 1;
  521. offset[1] = 2;
  522. count[0] = NX_SUB;
  523. count[1] = NY_SUB;
  524. status = H5Sselect_hyperslab(dataspace,H5S_SELECT_SET,offset,count,NULL);
  525.  
  526. /*
  527. * Define the memory dataspace.
  528. */
  529. dimsm[0] = NX;
  530. dimsm[1] = NY;
  531. dimsm[2] = NZ;
  532. memspace = H5Screate_simple(RANK_OUT,dimsm,NULL);
  533.  
  534. /*
  535. * Define memory hyperslab.
  536. */
  537. offset_out[0] = 3;
  538. offset_out[1] = 0;
  539. offset_out[2] = 0;
  540. count_out[0] = NX_SUB;
  541. count_out[1] = NY_SUB;
  542. count_out[2] = 1;
  543. status = H5Sselect_hyperslab(memspace,offset_out,count_out,NULL);
  544.  
  545. /*
  546. * Read data from hyperslab in the file into the hyperslab in
  547. * memory and display.
  548. */
  549. status = H5Dread(dataset,memspace,data_out);
  550. for (j = 0; j < NX; j++) {
  551. for (i = 0; i < NY; i++) printf("%d ",data_out[j][i][0]);
  552. printf("\n");
  553. }
  554. /*
  555. * 0 0 0 0 0 0 0
  556. * 0 0 0 0 0 0 0
  557. * 0 0 0 0 0 0 0
  558. * 3 4 5 6 0 0 0
  559. * 4 5 6 7 0 0 0
  560. * 5 6 7 8 0 0 0
  561. * 0 0 0 0 0 0 0
  562. */
  563.  
  564. /*
  565. * Close/release resources.
  566. */
  567. H5Tclose(datatype);
  568. H5Dclose(dataset);
  569. H5Sclose(dataspace);
  570. H5Sclose(memspace);
  571. H5Fclose(file);
  572.  
  573. return 0;
  574. }
  575.  
  576. int test_HDF5()
  577. {
  578. //http://wenku.baidu.com/link?url=HDnqbrqqJ27GSvVGTcCbfM-bn5K2QCYxSlqTEtY_jwFvBVi3B97DK6s9qBUwXDjVgMHFQq-MLGSKcMKeGJkq87GF_8vchhsleRWISq9PwO3
  579. //http://baike.baidu.com/link?url=TqYZDUzu_XFMYa9XswMS1OVSyboWzu3RtK6L-dioFZT6zugtXjBUIFa4QHerxZcSbPNuTO84BomEGgxpchWojK
  580. //http://www.docin.com/p-608918978.html
  581. // 1-->./examples/h5_write.c:This example writes data to the HDF5 file
  582. test_HDF5_write_HDF5_Data();
  583. // 2-->./examples/h5_read.c:This example reads hyperslab from the SDS.h5 file
  584. test_HDF5_read_HDF5_data();
  585.  
  586. return 0;
  587. }
  588.  
  589. int test_Snappy()
  590. {
  591. //http://baike.baidu.com/link?url=X8PCUvwS0MFJF5xS2DdzMrVDj9hNV8VsXL40W_jgiI1DeGNW5q5PsfEbL9RwUSrIilseenbFiulT1ceONYL5E_
  592. //exaples:./snappy_unittest.cc、snappy-test.cc
  593. //https://snappy.angeloflogic.com/cpp-tutorial/
  594.  
  595. char* filename = "E:/tmp/snappy/fireworks.jpeg";
  596. size_t input_length = 200;
  597. snappy_status status;
  598. size_t output_length = snappy_max_compressed_length(input_length);
  599. char* output = (char*)malloc(output_length);
  600. status = snappy_compress(filename,input_length,output,&output_length);
  601. if (status != SNAPPY_OK) {
  602. std::cout << "snappy compress fail!" << std::endl;
  603. }
  604. free(output);
  605.  
  606. size_t output_length1;
  607. snappy_status status1;
  608. status1 = snappy_uncompressed_length(filename,&output_length1);
  609. if (status != SNAPPY_OK) {
  610. std::cout << "get snappy uncompress length fail!" << std::endl;
  611. }
  612. char* output1 = (char*)malloc(output_length1);
  613. status1 = snappy_uncompress(filename,output1,&output_length1);
  614. if (status != SNAPPY_OK) {
  615. std::cout << "snappy uncompress fail!" << std::endl;
  616. }
  617. free(output1);
  618.  
  619. return 0;
  620. }

猜你在找的设计模式相关文章