示例#1
0
/// Called by the application to terminate the scheduler and network.
void hpx_exit(int code) {
  dbg_assert_str(here->ranks,
                 "hpx_exit can only be called when the system is running.\n");

  uint64_t c = (uint32_t)code;

  // Make sure we flush our local network when we stop, but don't send our own
  // shutdown here because it can "arrive" locally very quickly, before we've
  // even come close to sending the rest of the stop commands. This can cause
  // problems with flushing.
  for (int i = 0, e = here->ranks; i < e; ++i) {
    if (i != here->rank) {
      int e = action_call_lsync(locality_stop, HPX_THERE(i), 0, 0, 1, &c);
      dbg_check(e);
    }
  }

  // Call our own shutdown through cc, which orders it locally after the effects
  // from the loop above.
  int e = hpx_call_cc(HPX_HERE, locality_stop, &c);
  hpx_thread_exit(e);
}
static int _nqueens_action(void *args, size_t size)
{
	int i, j;
	struct thread_data *my_data;
	my_data = (struct thread_data *) args;

	/*
	printf("n = %d, col = %d, count = %d\n", my_data->n
			, my_data->col
			, count);
	*/

	if (my_data->col == my_data->n) {
		hpx_lco_sema_p(mutex);
		++count;
		/*				
						printf("\nNo. %d\n-----\n", count);
						for (i = 0; i < my_data->n; i++, putchar('\n'))
						for(j = 0; j < my_data->n; j++)
						putchar(j == my_data->lyst[i] ? 'Q' : ((i + j) & 1) ? ' ' : '.');
						*/
		hpx_lco_sema_v_sync(mutex);

		hpx_thread_exit(HPX_SUCCESS);
		//hpx_thread_continue(NULL, 0);
		//return HPX_SUCCESS;
	}


#define p_attack(i, j) (my_data->lyst[j] == i || abs(my_data->lyst[j] - i) == my_data->col - j)

	int dummy=0;
	int num_spawns=0;
	for(i = 0, j = 0; i < my_data->n; i++) {
		for (j = 0; j < my_data->col && !p_attack(i, j); j++);
		if (j < my_data->col) {
			dummy++;
		}
	}
	//printf("dummy/spawns: %d/%d\n", dummy, my_data->n);

	num_spawns = my_data->n - dummy;
	bool D_CALL = false;

	//printf("num_spawns = %d\n", num_spawns);
	if( num_spawns == 0 ) {
		num_spawns = 1;
		D_CALL = true;
	}

	//num_spawns = my_data->n;
	struct thread_data temp[num_spawns];
	hpx_addr_t futures[num_spawns];
	hpx_addr_t threads[num_spawns];
	int pqs[num_spawns];
	size_t p_size[num_spawns];
	void *addrs[num_spawns];

	for(i = 0; i < num_spawns; i++) {
		futures[i] = hpx_lco_future_new(sizeof(int));
		threads[i] = HPX_HERE;
		pqs[i] = 0;
		addrs[i] = &pqs[i];
		p_size[i] = sizeof(size_t);
	}

	int k=0; // counter for hpx data
	for(i = 0, j = 0; i < my_data->n; i++) {
		for (j = 0; j < my_data->col && !p_attack(i, j); j++);
		if (j < my_data->col) {
			//printf("[%d] call continue.\n", i);
			continue;
		}
		//printf("[%d] call nqueens %d\n", i, k);

		my_data->lyst[my_data->col] = i;

		memcpy(temp[k].lyst, my_data->lyst, MAX_SIZE*sizeof(int));
		temp[k].n    = my_data->n;
		temp[k].col  = my_data->col+1;

		//solve(n, col + 1, hist);
		hpx_call(threads[k], _nqueens, futures[k], (void *)&temp[k], sizeof(temp[k]));
		k++;
	}

	if( !D_CALL ) {
		hpx_lco_get_all(num_spawns, futures, p_size, addrs, NULL);

		for(i = 0; i < num_spawns; i++)
			hpx_lco_delete(futures[i], HPX_NULL);
	}

	return HPX_SUCCESS;
}