예제 #1
0
float* irelief(char* matrix, char* label, int& t,int s,int f)
{
	samplecount = s;
	featurecount = f;
	msize = max(s, f);
	float* w_old = new float[featurecount];
	for (int  i = 0; i < featurecount; ++i)
	{
		w_old[i] = float(1.0)/featurecount;
	}
	float n_norm = norm_2(w_old);
	for (int i = 0; i < featurecount; ++i)
	{
		w_old[i] /= n_norm;
	}
	
	char** miss = new char*[msize];
	char** hit = new char*[msize];
	for (int i = 0; i < msize; ++i)
	{
		miss[i] = new char[msize];
		hit[i] = new char[msize];
	}
	cal(label, miss, hit);
	float theta = float(0.001);
	float* w;
	while (true)
	{
		w = compute_weight(matrix, w_old, miss, hit);
		float* temp = new float[featurecount];
		sub(w, w_old, temp);
		float stp = norm_2(temp);
		cout << "stp is "<< stp << endl;
		cout << find_max(w) << endl;
		if (stp < theta || t == 20)
			break;
		//free(w_old);
		w_old = w;
		++t;
		cout << t << endl;
	}

	for (int i = 0; i < samplecount; ++i)
	{
		delete[] hit[i];
		delete[] miss[i];
	}


	//free(miss);
	//free(hit);
	//free(w_old);

	return w;
}
예제 #2
0
/*Following function swap two encoding in a complete solution and compute the
new weight function, if it's less than previous one, the new encodings is kept.*/
boolean try_swap_fun(int ic,int jc,int cpog_count,int bits){
	int tmp;
	float wg = 0,wg_swapped = 0;

	wg = compute_weight(cpog_count, bits, counter);

	tmp = perm[counter][ic];
	perm[counter][ic] = perm[counter][jc];
	perm[counter][jc] = tmp;
	
	wg_swapped = compute_weight(cpog_count, bits, counter);

	if(wg <= wg_swapped){
		tmp = perm[counter][ic];
		perm[counter][ic] = perm[counter][jc];
		perm[counter][jc] = tmp;
		return FALSE;
	}
	return TRUE;
}
예제 #3
0
  void compute_DG0_to_CG_weight_matrix(GenericMatrix& A, Function& DG)
  {
    compute_weight(DG);

    std::vector<std::size_t> columns;
    std::vector<double> values;
    std::vector<std::vector<std::size_t> > allcolumns;
    std::vector<std::vector<double> > allvalues;
        
    const std::pair<std::size_t, std::size_t> row_range = A.local_range(0);
    const std::size_t m = row_range.second - row_range.first;
    GenericVector& weight = *DG.vector();
    const std::pair<std::size_t, std::size_t> weight_range = weight.local_range();
    std::vector<std::size_t> weight_range_vec(2);
    weight_range_vec[0] = weight_range.first;
    weight_range_vec[1] = weight_range.second;
    int dm = weight_range.second-weight_range.first;
    
    const MPI_Comm mpi_comm = DG.function_space()->mesh()->mpi_comm();
        
    // Communicate local_ranges of weights
    std::vector<std::vector<std::size_t> > all_ranges;
    MPI::all_gather(mpi_comm, weight_range_vec, all_ranges);
    
    // Number of MPI processes
    std::size_t num_processes = MPI::size(mpi_comm);

    // Some weights live on other processes and need to be communicated
    // Create list of off-process weights
    std::vector<std::vector<std::size_t> > dofs_needed(num_processes);    
    for (std::size_t row = 0; row < m; row++)
    {   
      // Get global row number
      const std::size_t global_row = row + row_range.first;
      
      A.getrow(global_row, columns, values);
      
      for (std::size_t i = 0; i < columns.size(); i++)
      {
        std::size_t dof = columns[i];
        if (dof < weight_range.first || dof >= weight_range.second)
        {
          std::size_t owner = dof_owner(all_ranges, dof);
          dofs_needed[owner].push_back(dof);
        }
      }
    }

    // Communicate to all which weights are needed by the process
    std::vector<std::vector<std::size_t> > dofs_needed_recv;
    MPI::all_to_all(mpi_comm, dofs_needed, dofs_needed_recv);
    
    // Fetch the weights that must be communicated
    std::vector<std::vector<double> > weights_to_send(num_processes);    
    for (std::size_t p = 0; p < num_processes; p++)
    {
      if (p == MPI::rank(mpi_comm))  
        continue;
      
      std::vector<std::size_t> dofs = dofs_needed_recv[p];
      std::map<std::size_t, double> send_weights;
      for (std::size_t k = 0; k < dofs.size(); k++)
      {
        weights_to_send[p].push_back(weight[dofs[k]-weight_range.first]);
      }
    }
    std::vector<std::vector<double> > weights_to_send_recv;
    MPI::all_to_all(mpi_comm, weights_to_send, weights_to_send_recv);
    
    // Create a map for looking up received weights
    std::map<std::size_t, double> received_weights;
    for (std::size_t p = 0; p < num_processes; p++)
    {
      if (p == MPI::rank(mpi_comm))
        continue;
      
      for (std::size_t k = 0; k < dofs_needed[p].size(); k++)
      {
        received_weights[dofs_needed[p][k]] = weights_to_send_recv[p][k];         
      }
    }
    
    for (std::size_t row = 0; row < m; row++)
    {   
      // Get global row number
      const std::size_t global_row = row + row_range.first;
      
      A.getrow(global_row, columns, values);
      for (std::size_t i = 0; i < values.size(); i++)
      {
        std::size_t dof = columns[i];
        if (dof < weight_range.first || dof >= weight_range.second)
        {
          values[i] = received_weights[dof];
        }
        else
        {
          values[i] = weight[columns[i]-weight_range.first];  
        }
//        values[i] = 1./values[i];
      }
      
      double s = std::accumulate(values.begin(), values.end(), 0.0);
      std::transform(values.begin(), values.end(), values.begin(),
                     std::bind2nd(std::multiplies<double>(), 1./s));      

      for (std::size_t i=0; i<values.size(); i++)
      {
        double w;
        std::size_t dof = columns[i];
        if (dof < weight_range.first || dof >= weight_range.second)
        {
          w = received_weights[dof];
        }
        else
        {
          w = weight[dof-weight_range.first];  
        }        
        values[i] = values[i]*w;
//        values[i] = values[i]*values[i];
        
      }     
      
      allvalues.push_back(values);
      allcolumns.push_back(columns);
    }

    for (std::size_t row = 0; row < m; row++)
    {       
      // Get global row number
      const std::size_t global_row = row + row_range.first;
      
      A.setrow(global_row, allcolumns[row], allvalues[row]);
    }
    A.apply("insert");  
  }  
예제 #4
0
/**
 * Constructs an interface graph from all local modules and the given proc's
 * remote interfaces.  The resulting vertices will always have the module
 * vertices appear before the proc vertices.
 */
static int create_proc_module_graph(
    opal_btl_usnic_proc_t *proc,
    bool proc_is_left,
    opal_btl_usnic_graph_t **g_out)
{
    int err;
    int i, j;
    int u, v;
    int num_modules;
    opal_btl_usnic_graph_t *g = NULL;

    if (NULL == g_out) {
        return OPAL_ERR_BAD_PARAM;
    }
    *g_out = NULL;

    num_modules = (int)mca_btl_usnic_component.num_modules;

    /* Construct a bipartite graph with remote interfaces on the one side and
     * local interfaces (modules) on the other. */
    err = opal_btl_usnic_gr_create(NULL, NULL, &g);
    if (OPAL_SUCCESS != err) {
        OPAL_ERROR_LOG(err);
        goto out;
    }

    /* create vertices for each interface (local and remote) */
    for (i = 0; i < num_modules; ++i) {
        int idx = -1;
        err = opal_btl_usnic_gr_add_vertex(g,
                                           mca_btl_usnic_component.usnic_active_modules[i],
                                           &idx);
        if (OPAL_SUCCESS != err) {
            OPAL_ERROR_LOG(err);
            goto out_free_graph;
        }
        assert(idx == MODULE_VERTEX(i));
    }
    for (i = 0; i < (int)proc->proc_modex_count; ++i) {
        int idx = -1;
        err = opal_btl_usnic_gr_add_vertex(g, &proc->proc_modex[i], &idx);
        if (OPAL_SUCCESS != err) {
            OPAL_ERROR_LOG(err);
            goto out_free_graph;
        }
        assert(idx == (int)PROC_VERTEX(i));
    }

    /* now add edges between interfaces that can communicate */
    for (i = 0; i < num_modules; ++i) {
        for (j = 0; j < (int)proc->proc_modex_count; ++j) {
            int64_t weight, cost;

            /* assumption: compute_weight returns the same weight on the
             * remote process with these arguments (effectively) transposed */
            weight = compute_weight(mca_btl_usnic_component.usnic_active_modules[i],
                                    &proc->proc_modex[j]);

            opal_output_verbose(20, USNIC_OUT,
                                "btl:usnic:%s: weight=0x%016" PRIx64 " for edge module[%d] (%p) <--> endpoint[%d] on proc %p",
                                __func__,
                                weight, i,
                                (void *)mca_btl_usnic_component.usnic_active_modules[i],
                                j, (void *)proc);

            if (WEIGHT_UNREACHABLE == weight) {
                continue;
            } else {
                /* the graph code optimizes for minimum *cost*, but we have
                 * been computing weights (negative costs) */
                cost = -weight;
            }
            assert(INT64_MAX != cost);
            assert(INT64_MIN != cost);

            if (proc_is_left) {
                u = PROC_VERTEX(j);
                v = MODULE_VERTEX(i);
            } else {
                u = MODULE_VERTEX(i);
                v = PROC_VERTEX(j);
            }
            opal_output_verbose(20, USNIC_OUT,
                                "btl:usnic:%s: adding edge (%d,%d) with cost=%" PRIi64 " for edge module[%d] <--> endpoint[%d]",
                                __func__, u, v, cost, i, j);
            err = opal_btl_usnic_gr_add_edge(g, u, v, cost,
                                             /*capacity=*/1,
                                             /*e_data=*/NULL);
            if (OPAL_SUCCESS != err) {
                OPAL_ERROR_LOG(err);
                goto out_free_graph;
            }
        }
    }

    *g_out = g;
    return OPAL_SUCCESS;

out_free_graph:
    opal_btl_usnic_gr_free(g);
out:
    return err;
}