我正在编写一个OpenMPI程序,它为每个处理器创建一个数据库实例,插入一些数据,并在完成时关闭数据库。该程序在一个处理器上运行良好。启动多个数据库实例会在创建第二个数据库实例时导致段错误。我的MPI程序看起来像这样:
#include <mpi.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "rocksdb_c.h"
int main (int argc, char *argv[]) {
MPI_Init(&argc, &argv);
int rank, size;
time_t t;
MPI_Comm_rank (MPI_COMM_WORLD, &rank); /* get current process id */
MPI_Comm_size (MPI_COMM_WORLD, &size); /* get number of processes */
srand(rank * 1766+8765);
int key = 0, value = 0;
rocksdb_env_t* env= CreateEnvironment(size);
rocksdb_options_t* options= InstantiateOptions(env);
rocksdb_t *db=CreateDb(options);
rocksdb_writeoptions_t* writeoptions=WriteOptions();
int i;
for ( i = 1; i < 10000; ++i) {
value = rand()%10000;
//key = rand()%10000;
key = i*size +rank;
Put(ToCharArray(key),ToCharArray(value), db, writeoptions);
printf("i=%d, key=%d, value=%d, rank=%d,size=%dn",i, key,value,rank,size);
}
CloseDb(db);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
我的rocksdb_c.h
是这样的:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include "rocksdb/c.h"
#include <unistd.h> // sysconf() - get CPU count
const char DBPath[]="/tm/rocksdb_simple_example";
const char DBBackupPath[] = "/tmp/rocksdb_simple_example_backup";
rocksdb_env_t* CreateEnvironment(int processes){
rocksdb_env_t* env=rocksdb_create_default_env();
rocksdb_env_set_background_threads(env,processes);
rocksdb_env_set_high_priority_background_threads(env, processes);
return env;
}
rocksdb_t* CreateDb(rocksdb_options_t* options){
rocksdb_t *db;
char *err=NULL;
db = rocksdb_open(options, DBPath, &err); assert(!err);
return db; }
void CloseDb(rocksdb_t *db){
rocksdb_close(db);
}
rocksdb_options_t* InstantiateOptions(int processes){
rocksdb_options_t* options= rocksdb_options_create();
rocksdb_options_set_create_if_missing(options, 1);
rocksdb_options_set_env(options,env);
return options;
}
rocksdb_writeoptions_t* WriteOptions(){
return rocksdb_writeoptions_create();
}
void Put(char* key, char* value, rocksdb_t* db, rocksdb_writeoptions_t* writeoptions){
char *err=NULL;
printf("beforen");
rocksdb_put(db, writeoptions, key, strlen(key), value, strlen(value) , &err);
}
程序在第二次调用CloseDB()
函数中的rocksdb_open()
时失败。错误列表分割失败。
任何想法?
我认为您需要为每个实例使用不同的DBPath
,并且您可能希望查看Env
,并可能在实例之间共享它。