Пример #1
0
int rm_block(char *dir, sha1_t key) {
    LOG_DEBUG(TAG_FILE, "Removing block");
    char fullpath[MAX_PATH_LEN];
    form_path(dir, fullpath, key);
    return remove(fullpath);

}
Пример #2
0
int read_block(char *dir, sha1_t key, byte *buf, int buflen) {
    LOG_DEBUG(TAG_FILE, "Reading block");
    char fullpath[MAX_PATH_LEN];
    form_path(dir, fullpath, key);
    int status;

    if ((status = create_dir(dir)) == 0) {
        FILE *file = fopen(fullpath, "rb");
        if (file == NULL) {
            LOG_ERROR(TAG_FILE, "%s", strerror(errno));
            return -1;
        } else {
            int bytes_read = fread(buf, sizeof(byte), buflen, file);
            fclose(file);
            return bytes_read;
        }
    } else {
        return -1;
    }
    
}
vector< vector<double> > calc_delta_slim(Graph& G, v_size_t diam)
{


  //Variable initializations
  const vector<double> zero_dist(diam+2,0);
  double maxDelta = 0;


  // for(tie(vit,vite)=vertices(G); vit!=vite; vit++)
  //   {
  //     Vert v =*vit;
  //     vert_vec.push_back(v);
  //   }


  vector< vector<double> > delta(diam,zero_dist);



#pragma omp parallel shared(delta,maxDelta,diam)
  {

    //Variable assignements,
    //Notation: puv = "Path indices on side uv"
    //           uv = "Vertices on side uv"
    //vertex iterators
    graph_traits<Graph>::vertex_iterator vit, vite;
  
    //Property maps
    property_map<Graph,vertex_index_t>::type index = get(vertex_index,G);

    vector<Vert> vert_vec;
    vector< vector<v_size_t> > d;

    for(tie(vit,vite) = vertices(G); vit!= vite; ++vit)
      {
	Vert v = *vit;
	vert_vec.push_back(v);
	d.push_back(G[v].distances);
      }

    const size_t size = vert_vec.size();

    vector<unsigned int> dummy;
    vector< vector<unsigned int> > puv(size,dummy);
    vector< vector<unsigned int> > puw(size,dummy);
    vector< vector<unsigned int> > pvw(size,dummy);

    vector<path_node> node_stack;
    node_stack.reserve(CHUNKSIZE);

    for(size_t i=0;i<size;i++)
      {
    	puv[i].reserve(CHUNKSIZE);
    	puw[i].reserve(CHUNKSIZE);
    	pvw[i].reserve(CHUNKSIZE);
	
      }
    
    vector<unsigned int> delta_v;
    vector<unsigned int> k1;
    vector<unsigned int> k2;

    delta_v.reserve(diam+1);
    k1.reserve(CHUNKSIZE);
    k2.reserve(CHUNKSIZE);

    vector<Vert> uv,uw,vw;
    vector<bool> on_path;
    queue<Vert> Q;

    on_path.reserve(size);
    uv.reserve(diam+1);
    uw.reserve(diam+1);
    vw.reserve(diam+1);

    vector<vector<Vert> > vPredecessors;
    vector<vector<Vert> > uPredecessors;

    vector< vector<double> > delta_loc(diam,zero_dist);
    double maxDelta_loc = 0;

    double uvDelta, uwDelta, vwDelta, currDelta;
    unsigned int nuv, nuw, nvw;
    
    //distribute outer for loops
#pragma omp for schedule(dynamic,1)
    for(size_t i=0;i<size;i++)
      {
	const Vert u = vert_vec[i];
	const v_size_t ui = index[u];
	
#pragma omp single nowait
	{
	  cerr<<"Vertex: "<<u<<"\n";
	}

	uPredecessors = G[u].predecessors;
	//cerr<<"Vertex: "<<u<<"\n";

	for(size_t j=i+1;j<size;++j)
	  {
	    const Vert v = vert_vec[j];
	    const v_size_t vi = index[v];
	    const v_size_t uvd = d[ui][vi];
	  
	    vPredecessors = G[v].predecessors;
	    form_path(u,v,G,uPredecessors, uv, Q, on_path, index);

	    // if(j==(size-1))
	    //   {
	    // 	for(size_t k=0;k<size;k++)
	    // 	  {
	    // 	    puv[k].clear();
	    // 	    puw[k].clear();
	    // 	    pvw[k].clear();
		    
	    // 	    uv_meta[k][1] = 0;
	    // 	    uw_meta[k][1] = 0;
	    // 	    vw_meta[k][1] = 0;
	    // 	  }
	    //   }
	    
	    for(size_t k=j+1;k<size;++k)
	      {
		const Vert w = vert_vec[k];
		const v_size_t wi = index[w];

		const v_size_t uwd = d[ui][wi];
		const v_size_t vwd = d[vi][wi];

		v_size_t maxSide = uvd;

		if(uwd>maxSide)
		  maxSide = uwd;

		if(vwd>maxSide)
		  maxSide = vwd;
		
		  form_path(u,w,G,uPredecessors,uw,Q,on_path,index);
		  form_path(v,w,G,vPredecessors,vw,Q,on_path,index);
		
		//**Code commented out for BFS (less memory, MUCH slower version)**
		//BFS_two_target(u,v,w,G,uv,uw,predecessors);
	      
		// vector< vector<unsigned int> > puv;
		// vector< vector<unsigned int> > puw;
		// vector< vector<unsigned int> > pvw;


		  //#pragma omp parallel sections num_threads(3)
		    //{
		  //#pragma omp section
		  nuv = index_paths(u,v,index,size,uPredecessors,puv,node_stack);
		  //#pragma omp section		
		  nuw = index_paths(u,w,index,size,uPredecessors,puw,node_stack);
		  //#pragma omp section		
		  nvw = index_paths(v,w,index,size,vPredecessors,pvw,node_stack);	      
		  //}
		//**Debugging commented out**
		// cout<<"u: "<<u<<" v: "<<v<<"\n";
		// for(int l=0;l<uv.size();l++)
		// 	{
		// 	  Vert c = uv[l];
		// 	  v_size_t ci = index[c];
		// 	  cout<<c<<"\nOn Paths: ";
		// 	  for(int z=0;z<puv[ci].size();z++)
		// 	    {
		// 	      cout<<puv[ci][z]<<" ";
		// 	    }
		// 	  cout<<"\n";
		// 	}
		// char a;
		// cin>>a;	  
  
		//**Again, BFS code**
		//BFS_source_target(v,w,G,vw,predecessors);


		//Calculates delta for each side, ie returns the maximum minimum distance for 
		//any vertex on a shortest path between the pair 

		  //#pragma omp parallel sections shared(uvDelta,uwDelta,vwDelta) num_threads(3)
		    //{
		  //#pragma omp section
		  uvDelta = pair_delta(d,uv,uw,vw,puw,pvw,nuw,nvw,index,delta_v,k1,k2,size);
		  //#pragma omp section
		  uwDelta = pair_delta(d,uw,uv,vw,puv,pvw,nuv,nvw,index,delta_v,k1,k2,size);
		  //#pragma omp section
		  vwDelta = pair_delta(d,vw,uv,uw,puv,puw,nuv,nuw,index,delta_v,k1,k2,size);
		  //}
		//The worst of all pairs is the delta of that triplet
		currDelta = uvDelta;
		if(uwDelta>currDelta)
		  currDelta = uwDelta;
		if(vwDelta>currDelta)
		  currDelta = vwDelta;

		++delta_loc[maxSide-1][currDelta+1];
		if(currDelta>maxDelta_loc)
		  maxDelta_loc = currDelta;

		++delta_loc[maxSide-1][0]; 
	      }
	  }
      }

    #pragma omp critical
    {
      //cout<<delta_loc.size()<<"\n";
      for(size_t i=0;i<delta_loc.size();++i)
	for(size_t j=0;j<delta_loc[i].size();++j)
	  {
	    //cout<<i<<" "<<j<<"\n";
	    delta[i][j] += delta_loc[i][j];
	  }

      if(maxDelta_loc > maxDelta)
	maxDelta = maxDelta_loc;      
    }

  }
  for(int i=0;i<diam;++i)
    {
      delta[i].erase(delta[i].begin()+maxDelta+2,delta[i].end());
    }

  return(delta);
}
vector< vector<double> > calc_delta_fat(Graph& G, v_size_t diam)
{


  //Variable initializations
  v_size_t size = num_vertices(G);
  v_size_t biggest_delta = 3*diam;

  const vector<double> zero_dist(biggest_delta+2,0);
  double maxDelta = 0;


  // for(tie(vit,vite)=vertices(G); vit!=vite; vit++)
  //   {
  //     Vert v =*vit;
  //     vert_vec.push_back(v);
  //   }



  vector< vector<double> > delta(diam,zero_dist);



#pragma omp parallel shared(delta,maxDelta,diam,size)
  {

    //Variable assignements,
    //Notation: puv = "Path indices on side uv"
    //           uv = "Vertices on side uv"
    //vertex iterators
    graph_traits<Graph>::vertex_iterator vit, vite;
  
    //Property maps
    property_map<Graph,vertex_index_t>::type index = get(vertex_index,G);

    vector<Vert> vert_vec;
    vector< vector<v_size_t> > d;

    for(tie(vit,vite) = vertices(G); vit!= vite; ++vit)
      {
	Vert v = *vit;
	vert_vec.push_back(v);
	d.push_back(G[v].distances);
      }

    //    const size_t size = vert_vec.size();

    vector<unsigned int> dummy;
    
    vector<Vert> uv,uw,vw;
    vector<bool> on_path;
    vector<Vert> Q;

    Q.reserve(2*diam);
    on_path.reserve(size);
    uv.reserve(diam+1);
    uw.reserve(diam+1);
    vw.reserve(diam+1);

    vector<vector<Vert> > vPredecessors;
    vector<vector<Vert> > uPredecessors;

    vector< vector<double> > delta_loc(diam,zero_dist);
    double maxDelta_loc = 0;
    
    double currDelta = size;
    size_t uvi,uwi,vwi;
    
    Vert x,y,z;
    v_size_t xi,yi,zi;
    v_size_t xyd,yzd,xzd;
    v_size_t sum;

    //distribute outer for loops
#pragma omp for schedule(dynamic,1)
    for(size_t i=0;i<size;++i)
      {
	const Vert u = vert_vec[i];
	const v_size_t ui = index[u];
	
#pragma omp single nowait
	{
	  cerr<<"Vertex: "<<u<<"\n";
	}

	uPredecessors = G[u].predecessors;
	//cerr<<"Vertex: "<<u<<"\n";

	for(size_t j=i+1;j<size;++j)
	  {
	    const Vert v = vert_vec[j];
	    const v_size_t vi = index[v];
	    const v_size_t uvd = d[ui][vi];
	  
	    vPredecessors = G[v].predecessors;
	    form_path(u,v,G,uPredecessors, uv, Q, on_path, index);

	    // if(j==(size-1))
	    //   {
	    // 	for(size_t k=0;k<size;k++)
	    // 	  {
	    // 	    puv[k].clear();
	    // 	    puw[k].clear();
	    // 	    pvw[k].clear();
		    
	    // 	    uv_meta[k][1] = 0;
	    // 	    uw_meta[k][1] = 0;
	    // 	    vw_meta[k][1] = 0;
	    // 	  }
	    //   }
	    
	    for(size_t k=j+1;k<size;++k)
	      {
		const Vert w = vert_vec[k];
		const v_size_t wi = index[w];

		const v_size_t uwd = d[ui][wi];
		const v_size_t vwd = d[vi][wi];

		v_size_t maxSide = uvd;

		if(uwd>maxSide)
		  maxSide = uwd;

		if(vwd>maxSide)
		  maxSide = vwd;
		
		  form_path(u,w,G,uPredecessors,uw,Q,on_path,index);
		  form_path(v,w,G,vPredecessors,vw,Q,on_path,index);

		  //Finding the worst delta_fat triangle in each triplet
		  currDelta = size;
		  for(uvi=0;uvi<uv.size();++uvi)
		    {
		      x  = uv[uvi];
		      xi = index[x];
		      for(uwi=0;uwi<uw.size();++uwi)
			{
			  y   = uw[uwi];
			  yi  = index[y];
			  xyd = d[xi][yi];
			  for(vwi=0;vwi<vw.size();++vwi)
			    {
			      z   = vw[vwi];
			      zi  = index[z];
			      yzd = d[yi][zi];
			      xzd = d[xi][zi];
			      sum = xyd+yzd+xzd;

			      //Is there a way to confirm the load balancing issue on this?  For example, 
			      //I could set the predecessors to a fixed set of nodes and see what happens each time?
			      //I could give everyone a set of the predecessor matrix and see how it scales in that case.

			      if(currDelta > sum)
				currDelta = sum;
			      
			    }
			}
		    }

		++delta_loc[maxSide-1][currDelta+1];
		if(currDelta>maxDelta_loc)
		  maxDelta_loc = currDelta;

		++delta_loc[maxSide-1][0]; 
	      }
	  }
      }

    #pragma omp critical
    {
      //cout<<delta_loc.size()<<"\n";
      for(size_t i=0;i<delta_loc.size();++i)
	for(size_t j=0;j<delta_loc[i].size();++j)
	  {
	    //cout<<i<<" "<<j<<"\n";
	    delta[i][j] += delta_loc[i][j];
	  }

      if(maxDelta_loc > maxDelta)
	maxDelta = maxDelta_loc;      
    }

  }
  for(size_t i=0;i<diam;++i)
    {
      delta[i].erase(delta[i].begin()+maxDelta+2,delta[i].end());
    }

  return(delta);
}
Пример #5
0
int main(int argc, char **argv){
        size_t f_size=0;//f_size need for map
        struct dirent **read_dir;
        DIR *dir;
        int msg_id;
        key_t msg_key;


        struct file_data_struct **file_data;
        struct stat *buf;
        // f_num - number of file in directiry
        // fd - file descriptor
        int fd, f_num = 0;
        void *for_map, *h_for_map;
        buf = (struct stat*)malloc(sizeof(struct stat));
        if( argc!=2)
                my_error("Incorrect work excl\n");
        // Open dir
        read_dir = (struct dirent**)malloc(sizeof(struct dirent*));
        file_data = (struct file_data_struct **) malloc (sizeof (struct file_data_struct*));
        if ((dir = opendir(argv[1])) == NULL)
                 my_error ("Can't open dir!\n");
        //Get information
        char* path;
        while ( (read_dir[f_num] = readdir(dir)) != NULL ){
               if ((strcmp ((read_dir[f_num])->d_name, ".") != 0) && (strcmp((read_dir[f_num])->d_name, ".." ) != 0)){
                        f_num ++;
                        file_data = (struct file_data_struct**) realloc (file_data, (f_num + 1) * sizeof(struct file_data_struct));
                        read_dir = (struct dirent**) realloc (read_dir, (f_num + 1) * sizeof(struct dirent*));
                        path = form_path(argv[1], (read_dir[f_num - 1]) -> d_name);
                        if (lstat(path, buf) < 0)
                                my_error("error in stat!\n");
                        get_info(file_data, buf, (read_dir[f_num - 1])->d_name, f_num - 1);
                }
        }
        //print all info in file
        int i = 0;
        for ( i=0; i < f_num; i++ )
                f_size += (file_data[i])->str_len;
        f_size += sizeof(size_t);
        f_size += sizeof(int);
        for_map = open_file(f_size);
        h_for_map = for_map;
        *((size_t* )h_for_map) = f_size;
        (h_for_map) += sizeof(size_t);
        * ((int * ) h_for_map ) = f_num;
        (h_for_map) += sizeof(int);
        for ( i=0 ; i < f_num ; i++) {
                * ((struct file_data_struct * ) h_for_map )= * (file_data[i]) ;
                strcpy ( ((struct file_data_struct * ) h_for_map ) -> nug, (file_data[i])-> nug);
                h_for_map += (file_data[i])->str_len ;

        }
        //close dir
        if ( closedir(dir) != 0)
                my_error ("ERROR in close dir!\n");
        if ( munmap (for_map, f_size + sizeof (int) ) < 0 )
                my_error ("Error in munap!\n");
        free(path);
        free(buf);
        
        struct mymsgbuf {
                long mtype;
                size_t file_len;
        } my_msg;

        my_msg.mtype = 1;
        my_msg.file_len = f_size;

        msg_key = ftok (KEYFILE , 1);
        if (msg_key < 0)
                my_error ("ERROR in ftok!\n");

        msg_id = msgget (msg_key, IPC_CREAT | 0666);
        if (msg_id < 0)
                my_error ("ERROR in msgget!\n");

        if (msgsnd (msg_id, &my_msg , sizeof (size_t) , 0) < 0) {
                msgctl (msg_id , IPC_RMID , NULL );
                my_error ("Can not send a message!\n");
        }

        
        return 0;
}