示例#1
0
文件: array.c 项目: peterlintang/apue
int split_array(array_t *orig, int ix, array_t **first, array_t **second)
{
	if (!orig || ix >= orig->count || ix < 0 
		|| !first || !second) return -1;	/* error */

	(*second) = new_array();
	if (!(*second)) return 0;

	(*second)->count = orig->count - ix;
	(*second)->items = malloc(sizeof(void *) * (*second)->count);
	if (!(*second)->items) {
		destroy_array(second, NULL);
		return 0;
	} else {
		memcpy(&(*second)->items, &orig->items[ix], 
			sizeof(void *) * (*second)->count);
	}

	(*first) = orig;
	(*first)->count = ix;
	(*first)->items = realloc(orig->items, sizeof(void *) * ix);
	if (!(*first)->items) {
		destroy_array(second, NULL);
		return 0;
	}

	return 1;
}
示例#2
0
文件: array.c 项目: roma-turkin/array
int destroy_array(ARRAY array){
	if (!array){
		return -1; 				//Нам скормили NULL
	}
	struct node * left, *right, *root;
    root = (struct node *) array;
	if (root->index == -1 && !root->left)
	{
        free(root);
		return 0;
	}
	left = root->left;
	right = root->right;
	if (left)
	{					//Спускаемся только в те ветки, где что-то есть. Там имеет смысл освобождать что-либо
		destroy_array(left);	//Ну а здесь зарыта вся рекурсия.
		free(left);				//После того, как все элементы слева от текущего элемента освобождены, можно освобождать и текущий.
	}
	if (right)	
	{
		destroy_array(right);
		free(right);
	}
	//root->left = NULL;
	//root->right = NULL;
    if (root->index == -1) free(root);
    root = NULL;
	return 0;
}
示例#3
0
	inline void PacketRouter::destroy () noexcept {
	
		destroy_array(play_routes);
		destroy_array(status_routes);
		destroy_array(login_routes);
		destroy_array(handshake_routes);
	
	}
示例#4
0
char *server_dispatch(struct nli *nli)
{

	// put incoming command into array
	int ac 		= 0;
	int *acp 	= &ac;
	char **av 	= explode(nli->info->command, " ", acp); 

	char *rmt 	= NULL;
	rmt 		= filter_specified_remote(ac, av, 0);

	// find out what group the command needs to be sent to
	int who		= filter_who(ac, av, 0, rmt);


	// put the incoming command in the expected order
	char *buf 	= NULL;
	buf		= sanitize_command(ac, av, 0);
	if (buf == NULL) {
		return strdup("no command");
	}
	set_node_element(&nli->info->command, buf);

	// free all memory of the first explode array
	destroy_array(av, ac);

	// put sanitized command into array
	ac 		= 0;
	av		= explode(buf, " ", acp);

	// filter the destination out of the command
	char *cmd	= NULL;
	cmd 		= filter_command(ac, av, 0);

	// free all memory of the second explode array
	destroy_array(av, ac);


	// if nli->info isn't in rec_head nodelist
	// run the command or 
	// send it to the right nodes
	char *r, *uq;
	if (find_node(nli, rec_head) == NULL) {
		append_to_received_command_list(nli->info);
		uq = strdup(nli->info->unique);
		r = route_command(nli, who, rmt, cmd, buf, uq);
		free(uq);
	} else {
		r = strdup("Already have it"); 	
	}

	if (rmt) free(rmt);
	if (buf) free(buf);
	if (cmd) free(cmd);

	return r;
}
/**
 * Destroys the pointer internals.
 *
 * @param p0 the transient model
 * @param p1 the transient model size
 */
void destroy_pointer_internals(void* p0, const void* p1) {

//??    log_message((void*) &INFO_LOG_LEVEL, (void*) &DESTROY_INTERNALS_MESSAGE, (void*) &DESTROY_INTERNALS_MESSAGE_COUNT);

    // Destroy pointer internals.
    destroy_array(p0, (void*) &POINTER_ARRAY, p1);
}
示例#6
0
/**
 * Shows the message on screen.
 *
 * @param p0 the message
 * @param p1 the message size
 */
void show_message(void* p0, void* p1) {

    if (p1 != NULL_POINTER) {

        int* s = (int*) p1;

        // The log message.
        char* m = CHARACTER_NULL_POINTER;
        // The log size, including two places for new line and
        // string termination.
        int ls = *s + 2;

        create_array((void*) &m, (void*) &CHARACTER_ARRAY, (void*) &ls);

        // The destination index to which to copy the source array.
        int i = 0;
        char n = '\n';
        int ni = *s;
        char t = '\0';
        int ti = *s + 1;

        set_array_elements((void*) &m, (void*) &CHARACTER_ARRAY, (void*) &i, p0, p1);
        set_array_element((void*) &m, (void*) &CHARACTER_ARRAY, (void*) &ni, (void*) &n);
        set_array_element((void*) &m, (void*) &CHARACTER_ARRAY, (void*) &ti, (void*) &t);

        fputs(m, stdout);

        destroy_array((void*) &m, (void*) &CHARACTER_ARRAY, (void*) &ls);

    } else {

        fputs("Error: Could not show message. The message size is null.\n", stdout);
    }
}
示例#7
0
/**
 * Tests the integer parser.
 */
void test_integer_parser() {

    // The source string array, pointer, count.
    char ssa[] = {'1', '9', '7', '1'};
    void* ss = ssa;
    int ssc = 4;
    // The destination integer.
    int di = -1;
    int dic = -1;
    int dis = -1;

    parse((void*) &di, (void*) &dic, (void*) &dis, (void*) &ss, (void*) &ssc,
        (void*) &INTEGER_ABSTRACTION, (void*) &INTEGER_ABSTRACTION_COUNT);

    fprintf(stderr, "Parsed source string results in destination integer: %i\n", di);

    // The source integer.
    int si = 1980;
    int sic = -1;
    // The destination string.
    char* ds = NULL_POINTER;
    int dsc = 0;
    int dss = 0;

    // Create destination string.
    create_array((void*) &ds, (void*) &CHARACTER_ARRAY, (void*) &dss);

    serialize((void*) &ds, (void*) &dsc, (void*) &dss, (void*) &si, (void*) &sic,
        (void*) &INTEGER_ABSTRACTION, (void*) &INTEGER_ABSTRACTION_COUNT);

    fprintf(stderr, "Serialized source integer results in destination string: %s\n", ds);

    // Destroy destination string.
    destroy_array((void*) &ds, (void*) &CHARACTER_ARRAY, (void*) &dss);
}
/**
 * Destroys the vector.
 *
 * @param p0 the transient model
 */
void destroy_vector(void* p0) {

    log_message((void*) &INFO_LOG_LEVEL, (void*) &"Destroy vector.");

    // The vector.
    destroy_array(p0, (void*) &INTEGER_ARRAY, (void*) &VECTOR_COUNT);
}
示例#9
0
/*=================================================
 * array_destructor -- destructor for array
 *  (destructor entry in vtable)
 *===============================================*/
static void
array_destructor (VTABLE *obj)
{
	ARRAY array = (ARRAY)obj;
	ASSERT((*obj) == &vtable_for_array);
	destroy_array(array);
}
示例#10
0
/**
 * Destroys the time.
 *
 * @param p0 the transient model
 */
void destroy_time(void* p0) {

    log_message((void*) &INFO_LOG_LEVEL, (void*) &"Destroy time.");

    // The time.
    destroy_array(p0, (void*) &INTEGER_ARRAY, (void*) &TIME_COUNT);
}
示例#11
0
文件: devolver-array.c 项目: n-sm/qrs
 int main(int argc, char **argv) {
     int *array = create_array();
     for (size_t i = 0; i < 3; ++i)
         printf("%d\n", array[i]);
     destroy_array(array);
     return 0;
 }
/**
 * Destroys the double internals.
 *
 * @param p0 the transient model
 * @param p1 the transient model size
 */
void destroy_double_internals(void* p0, const void* p1) {

//??    log_message((void*) &INFO_LOG_LEVEL, (void*) &DESTROY_INTERNALS_MESSAGE, (void*) &DESTROY_INTERNALS_MESSAGE_COUNT);

    // Destroy double internals.
    destroy_array(p0, (void*) &DOUBLE_ARRAY, p1);
}
示例#13
0
/**
 * Tests the character array with multiple elements.
 */
void test_character_array_multiple_elements() {

    fputs("Test character array multiple elements:\n", stdout);

    // The destination array.
    void* d = NULL_POINTER;
    int ds = 22;

    // Create destination array.
    create_array((void*) &d, (void*) &ds, (void*) CHARACTER_ARRAY);

    // The source array.
    char a[] = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't', '.', '\n', '\0'};
    char* s = a;
    int ssa[] = {17};
    int* ss = ssa;

    // The destination index to which to copy the source array.
    set_character_array_elements(d, (void*) ZERO_NUMBER, (void*) s, (void*) ss);

    fputs((char*) d, stdout);

    // The source array for overwriting.
    char oa[] = {'o', 'v', 'e', 'r', 'w', 'r', 'i', 't', 't', 'e', 'n', '.', '\n', '\0'};
    char* os = oa;
    int ossa[] = {14};
    int* oss = ossa;

    set_character_array_elements(d, (void*) EIGHT_NUMBER, (void*) os, (void*) oss);

    fputs((char*) d, stdout);

    // The remove index.
    int ri = 12;

    remove_character_array_elements(d, (void*) &ds, (void*) &ri, (void*) SEVEN_NUMBER);

    fputs((char*) d, stdout);

    // The new array size to cut off remaining elements,
    // including two places for new line '\n' and c string termination '\0'.
    int ns = 15;

    resize_array((void*) &d, (void*) &ns, (void*) CHARACTER_ARRAY);

    fputs((char*) d, stdout);

    // The result array.
    void* r = NULL_POINTER;

    // Test getting a reference.
    get_character_array_elements(d, (void*) EIGHT_NUMBER, (void*) &r);

    fputs((char*) r, stdout);

    // Destroy destination array.
    destroy_array((void*) &d, (void*) &ns, (void*) CHARACTER_ARRAY);
}
示例#14
0
void
CAnimManager::Shutdown(void)
{
	for(int i = 0; i < CAnimManager::ms_numAnimations; i++)
		CAnimManager::ms_aAnimations[i].Shutdown();
	CAnimManager::ms_animCache->Shutdown();
	if(CAnimManager::ms_aAnimAssocGroups)
		destroy_array(CAnimManager::ms_aAnimAssocGroups, &CAnimBlendAssocGroup::dtor);
}
示例#15
0
/**
 * Tests the pointer array.
 */
void test_pointer_array() {

    fputs("Test pointer array:\n", stdout);

    //
    // Creation.
    //

    // The character array (including new line and null termination character).
    void* c = (void*) "Hello World!";
    int cs = 13;

    fprintf(stderr, "c: %s\n", (char*) c);

    // The pointer array.
    void** p = POINTER_NULL_POINTER;
    int ps = 1;

    // Create pointer array.
    create_array((void*) &p, (void*) &ps, (void*) POINTER_ARRAY);

    fprintf(stderr, "p: %i\n", p);

    // The result array.
    void** r = NULL_POINTER;

    //
    // Testing.
    //

    fprintf(stderr, "p[0] before set: %i\n", p[0]);
    fprintf(stderr, "p[1] before set: %i\n", p[1]);

    // Set character array in pointer array.
    // Hand over character array as reference, because pointer array is expected!
    set_array_elements(p, (void*) ZERO_NUMBER, (void*) &c, (void*) ONE_NUMBER, (void*) POINTER_ARRAY);

    fprintf(stderr, "p[0] after set: %i\n", p[0]);
    fprintf(stderr, "p[1] after set: %i\n", p[1]);

    // Get character array from pointer array.
    get_array_elements(p, (void*) ZERO_NUMBER, (void*) &r, (void*) POINTER_ARRAY);

    // Print result (character array).
    fprintf(stderr, "r pointer: %i\n", *r);
    fprintf(stderr, "r string: %s\n", (char*) *r);

    //
    // Destruction.
    //

    // Destroy pointer array.
    destroy_array((void*) &p, (void*) &ps, (void*) POINTER_ARRAY);
}
示例#16
0
文件: array.c 项目: dancheah/CeeCode
void run_experiment(ull testsize)
{
    array_t* a = new_array();

    for (ull i = 0; i < testsize; i++) {
        append(a, i);        
    }

    //print_array_info(a); 

    destroy_array(a);
}
示例#17
0
void			rebuild_paths(t_env *env)
{
	t_array		*path;

	path = array_get(env->env, "PATH=");
	if (env->paths)
		destroy_array(&env->paths);
	if (path)
		env->paths = convert_paths(path->data);
	else
		env->paths = NULL;
}
示例#18
0
文件: array.c 项目: peterlintang/apue
int merge_arrays(array_t **to, array_t **with)
{
	if (!to || !(*to)|| !with || !(*with)) return -1;

	(*to)->items = realloc((*to)->items, 
			sizeof(void *) * ((*to)->count + (*with)->count));
	if (!(*to)->items) return 0;

	memmove(&(*to)->items[(*to)->count], (*with)->items, 
		sizeof(void *) * (*with)->count);
	(*to)->count += (*with)->count;
	destroy_array(with, NULL);

	return (*to)->count;
}
示例#19
0
/**
 * Tests the character array with multiple elements.
 */
void test_character_array_multiple_elements() {

    // The destination array.
    void* d = NULL_POINTER;
    int ds = 20;
    create_array((void*) &d, (void*) &CHARACTER_ARRAY, (void*) &ds);

    // The constant source array and the pointer to it.
    char a[] = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't', '.', '\n', '\0'};
    char* s = a;
    int ss = 17;

    // The destination index to which to copy the source array.
    int i = 0;

    set_array_elements((void*) &d, (void*) &CHARACTER_ARRAY, (void*) &i, (void*) &s, (void*) &ss);

    fputs((char*) d, stdout);

    // The constant source array for overwriting and the pointer to it.
    char oa[] = {'o', 'v', 'e', 'r', 'w', 'r', 'i', 't', 't', 'e', 'n', '.', '\n', '\0'};
    char* os = oa;
    int oss = 14;

    // The destination index to which to copy the source array for overwriting.
    int oi = 8;

    set_array_elements((void*) &d, (void*) &CHARACTER_ARRAY, (void*) &oi, (void*) &os, (void*) &oss);

    fputs((char*) d, stdout);

    // The remove index.
    int ri = 12;
    // The remove count.
    int rc = 7;
    remove_array_elements((void*) &d, (void*) &CHARACTER_ARRAY, (void*) &ds, (void*) &ri, (void*) &rc);

    fputs((char*) d, stdout);

    // The new array size to cut off remaining elements,
    // including two places for new line '\n' and c string termination '\0'.
    int ns = 15;
    resize_array((void*) &d, (void*) &CHARACTER_ARRAY, (void*) &ns);

    fputs((char*) d, stdout);

    destroy_array((void*) &d, (void*) &CHARACTER_ARRAY, (void*) &ns);
}
示例#20
0
static inline void
destroy_container(JsonContainer container)
{
	switch (container->type) {
		case JSON_OBJECT:
			destroy_object(container->via.object);
			break;
		case JSON_ARRAY:
			destroy_array(container->via.array);
			break;
		case JSON_TOP_LEVEL:
			destroy_value(container->via.top_level.value);
			break;
	}
	pfree(container);
}
示例#21
0
/**
 * Tests the file read.
 */
void test_file_read() {

    // A file named "/home/cybop/tmp/test.cybol" needs to be created
    // in a text editor, for this test to work.

    // The array.
    void* a = NULL_POINTER;
    // The array size.
    int as = 0;
    // The array count.
    int ac = 0;
    // The file name array.
    char fna[] = {'/', 'h', 'o', 'm', 'e', '/', 'c', 'y', 'b', 'o', 'p', '/', 't', 'm', 'p', '/', 't', 'e', 's', 't', '.', 'c', 'y', 'b', 'o', 'l'};
    // The file name.
    char* fn = fna;
    // The file name count.
    int fnc = 26;

    create_array((void*) &a, (void*) &CHARACTER_ARRAY, (void*) &as);
//??    read_file((void*) &a, (void*) &as, (void*) &ac, (void*) &fn, (void*) &fnc);

    fprintf(stderr, "a: %i\n", a);
    fprintf(stderr, "as: %i\n", as);
    fprintf(stderr, "ac: %i\n", ac);
    fprintf(stderr, "fn: %i\n", fn);
    fprintf(stderr, "fnc: %i\n", fnc);

    int j = 0;
    char c = NULL_CHARACTER;

    while (1) {

        if (j >= ac) {

            break;
        }

        get_array_element((void*) &a, (void*) &CHARACTER_ARRAY, (void*) &j, (void*) &c);
        fputc(c, stdout);

        j++;
    }

    destroy_array((void*) &a, (void*) &CHARACTER_ARRAY, (void*) &as);
}
示例#22
0
文件: table.c 项目: GYGit/reactos
void free_row_values( const struct table *table, UINT row )
{
    UINT i, type;
    LONGLONG val;

    for (i = 0; i < table->num_cols; i++)
    {
        if (!(table->columns[i].type & COL_FLAG_DYNAMIC)) continue;

        type = table->columns[i].type & COL_TYPE_MASK;
        if (type == CIM_STRING || type == CIM_DATETIME)
        {
            if (get_value( table, row, i, &val ) == S_OK) heap_free( (void *)(INT_PTR)val );
        }
        else if (type & CIM_FLAG_ARRAY)
        {
            if (get_value( table, row, i, &val ) == S_OK)
                destroy_array( (void *)(INT_PTR)val, type & CIM_TYPE_MASK );
        }
    }
}
示例#23
0
/**
 * Tests the character array with a single element.
 */
void test_character_array_single_element() {

    fputs("Test character array single element:\n", stdout);

    // The character array.
    void* c = NULL_POINTER;
    int cs = 5;

    // Create character array.
    create_array((void*) &c, (void*) &cs, (void*) CHARACTER_ARRAY);

    set_character_array_elements(c, (void*) ZERO_NUMBER, (void*) LATIN_CAPITAL_LETTER_A_CHARACTER, (void*) LATIN_CAPITAL_LETTER_A_CHARACTER_COUNT);
    set_character_array_elements(c, (void*) ONE_NUMBER, (void*) LATIN_CAPITAL_LETTER_B_CHARACTER, (void*) LATIN_CAPITAL_LETTER_B_CHARACTER_COUNT);
    set_character_array_elements(c, (void*) TWO_NUMBER, (void*) LATIN_CAPITAL_LETTER_C_CHARACTER, (void*) LATIN_CAPITAL_LETTER_C_CHARACTER_COUNT);
    set_character_array_elements(c, (void*) THREE_NUMBER, (void*) LINE_FEED_CONTROL_CHARACTER, (void*) LINE_FEED_CONTROL_CHARACTER_COUNT);
    set_character_array_elements(c, (void*) FOUR_NUMBER, (void*) NULL_CONTROL_CHARACTER, (void*) NULL_CONTROL_CHARACTER_COUNT);

    // Print out array contents.
    fputs((char*) c, stdout);

    int i = 0;
    char* catest = CHARACTER_NULL_POINTER;

    while (1) {

        if (i >= cs) {

            break;
        }

        catest = (char*) (c + i);
        fprintf(stderr, "ca: %c\n", *catest);

        i++;
    }

    // Destroy character array.
    destroy_array((void*) &c, (void*) &cs, (void*) CHARACTER_ARRAY);
}
示例#24
0
void destroy_module(module_t* module, herror_t* perror)
{
    permissions_t* permissions;
    permissions_t* temp;

    permissions = module->permissions.next;
    while (permissions) {
        temp = permissions;
        permissions = permissions->next;
        if (temp->descr != NULL) {
            free(temp->descr);
        }
        free(temp);
    }
    free(module->permissions.descr);
    destroy_array(module->code, perror);
    if (*perror != E__NONE) {
        *perror |= E_I_ACCESSARRAY;
        return;
    }
    free(module);
}
static void test_sparse() {

    int *col_ind[MAXPROC];
    double *values[MAXPROC], *vec[MAXPROC], *svec[MAXPROC]/*, start_time*/;
    int n, non_zero, *row_ind;

    sparse_initialize(&n, &non_zero, &row_ind, col_ind, values, vec, svec);
    armci_msg_barrier();

    /*start_time = armci_timer();*/
    sparse_multiply(n, non_zero, row_ind, col_ind, values, vec, svec);
    /* printf("%d: Timetaken = %f\n", me, armci_timer()-start_time); */
    armci_msg_barrier();

    if(me==0) gather_solution_vector(svec);

    if(me==0) {
        printf("O.K.\n");
        fflush(stdout);
    }
    destroy_array((void **)vec);
}
示例#26
0
文件: test2.c 项目: arnolda/scafacos
void test_acc_type(const int datatype)
{
    int i = 0;
    int datatype_size = 0;
    void * scale;
    void * a;
    void *b[MAXPROC];
    int elems = ELEMS;
    int dim = 1;
    int count = 0;
    int strideA = 0;
    int strideB = 0;

    switch(datatype)
    {
       case ARMCI_ACC_INT:
          datatype_size = sizeof(int);
          scale = malloc(datatype_size);
          *((int *) scale) = 1;
          a = malloc(elems * datatype_size);
          create_array((void**)b, datatype_size, dim, &elems);
          for(i = 0; i < elems; i++)
          {
             ((int *) a)[i] = i + me;
             ((int *) b[me])[i] = 0;
          }
          break;
       case ARMCI_ACC_LNG:
          datatype_size = sizeof(long);
          scale = malloc(datatype_size);
          *((long *) scale) = 1;
          a = malloc(elems * datatype_size);
          create_array((void**)b, datatype_size, dim, &elems);
          for(i = 0; i < elems; i++)
          {
             ((long *) a)[i] = i + me;
             ((long *) b[me])[i] = 0;
          }
          break;
       case ARMCI_ACC_FLT:
          datatype_size = sizeof(float);
          scale = malloc(datatype_size);
          *((float *) scale) = 1.0;
          a = malloc(elems * datatype_size);
          create_array((void**)b, datatype_size, dim, &elems);
          for(i = 0; i < elems; i++)
          {
             ((float *) a)[i] = (float) i + me;
             ((float *) b[me])[i] = 0.0;
          }
          break;
       case ARMCI_ACC_DBL:
          datatype_size = sizeof(double);
          scale = malloc(datatype_size);
          *((double *) scale) = 1.0;
          a = malloc(elems * datatype_size);
          create_array((void**)b, datatype_size, dim, &elems);
          for(i = 0; i < elems; i++)
          {
             ((double *) a)[i] = (double) i + me;
             ((double *) b[me])[i] = 0.0;
          }
          break;
       case ARMCI_ACC_CPL:
          datatype_size = sizeof(cmpl_t);
          scale = malloc(datatype_size);
          ((cmpl_t *) scale)->real = 2.0;
          ((cmpl_t *) scale)->imag = 1.0;
          a = malloc(elems * datatype_size);
          create_array((void**)b, datatype_size, dim, &elems);
          for(i = 0; i < elems; i++)
          {
             ((cmpl_t *) a)[i].real = ((float) i + me);
             ((cmpl_t *) a)[i].imag = ((float) i + me);
             ((cmpl_t *) b[me])[i].real = 0.0;
             ((cmpl_t *) b[me])[i].imag = 0.0;
          }
          break;
       case ARMCI_ACC_DCP:
          datatype_size = sizeof(dcmpl_t);
          scale = malloc(datatype_size);
          ((dcmpl_t *) scale)->real = 2.0;
          ((dcmpl_t *) scale)->imag = 1.0;
          a = malloc(elems * datatype_size);
          create_array((void**)b, datatype_size, dim, &elems);
          for(i = 0; i < elems; i++)
          {
             ((dcmpl_t *) a)[i].real = ((double) i + me);
             ((dcmpl_t *) a)[i].imag = ((double) i + me);
             ((dcmpl_t *) b[me])[i].real = 0.0;
             ((dcmpl_t *) b[me])[i].imag = 0.0;
          }
          break;
       default:
          return;
          break;
    }

    count = elems * datatype_size;
    strideA = elems * datatype_size;
    strideB = elems * datatype_size;

    ARMCI_AllFence();
    MP_BARRIER();

    for(i = 0; i < nproc; i++)
       ARMCI_AccS(datatype, scale, a, &strideA, b[(me + i) % nproc], &strideB, &count, 0, (me + i) % nproc);

    ARMCI_AllFence();
    MP_BARRIER();

    switch(datatype)
    {
       case ARMCI_ACC_INT:
          for(i = 0; i < elems; i++)
          {
             int compare = (i * nproc) + nproc / 2 * (nproc - 1);
             if(((int *)b[me])[i] != compare) 
             {
                printf("ERROR accumulate ARMCI_ACC_INT [%d] = %d != %d\n", i, ((int *)b[me])[i], compare);
                ARMCI_Error("test_acc_type failed\n",0);
             }
          }
          break;
       case ARMCI_ACC_LNG:
          for(i = 0; i < elems; i++)
          {
             long compare = (i * nproc) + nproc / 2 * (nproc - 1);
             if(((long *)b[me])[i] != compare) 
             {
                printf("ERROR accumulate ARMCI_ACC_LNG [%d] = %d != %ld\n", i, ((int *)b[me])[i], compare);
                ARMCI_Error("test_acc_type failed\n",0);
             }
          }
          break;
       case ARMCI_ACC_FLT:
          for(i = 0; i < elems; i++)
          {
             float compare = (float) ((i * nproc) + nproc / 2 * (nproc - 1));
             if(((float *)b[me])[i] != compare) 
             {
                printf("ERROR accumulate ARMCI_ACC_FLT [%d] = %f != %f\n", i, ((float *)b[me])[i], compare);
                ARMCI_Error("test_acc_type failed\n",0);
             }
          }
          break;
       case ARMCI_ACC_DBL:
          for(i = 0; i < elems; i++)
          {
             double compare = (double) ((i * nproc) + nproc / 2 * (nproc - 1));
             if(((double *)b[me])[i] != (double) ((i * nproc) + nproc / 2 * (nproc - 1))) 
             {
                printf("ERROR accumulate ARMCI_ACC_DBL [%d] = %f != %f \n", i, ((double *)b[me])[i], compare);
                ARMCI_Error("test_acc_type failed\n",0);
             }
          }
          break;
       case ARMCI_ACC_CPL:
          for(i = 0; i < elems; i++)
          {
             float compare = (float) ((i * nproc) + nproc / 2 * (nproc - 1));
             if(((cmpl_t *)b[me])[i].real != compare && ((cmpl_t *)b[me])[i].imag != 3 * compare) 
             {
                printf("ERROR accumulate ARMCI_ACC_CPL [%d] = %f + %fj != %f + %fj\n", i, ((cmpl_t *)b[me])[i].real, ((cmpl_t *)b[me])[i].imag, compare, 3 * compare);
                ARMCI_Error("test_acc_type failed\n",0);
             }
          }
          break;
       case ARMCI_ACC_DCP:
          for(i = 0; i < elems; i++)
          {
             double compare = (double) ((i * nproc) + nproc / 2 * (nproc - 1));
             if(((dcmpl_t *)b[me])[i].real != compare && ((dcmpl_t *)b[me])[i].imag != 3 * compare) 
             {
                printf("ERROR accumulate ARMCI_ACC_DCP [%d] = %f + %fj != %f + %fj\n", i, ((dcmpl_t *)b[me])[i].real, ((dcmpl_t *)b[me])[i].imag, compare, 3 * compare);
                ARMCI_Error("test_acc_type failed\n",0);
             }
          }
          break;
       default:
          break;
    }

    MP_BARRIER();
    ARMCI_AllFence();
    MP_BARRIER();
    
    if(me==0){printf("O.K.\n\n"); fflush(stdout);}    
    destroy_array((void**)b);
    free(a);
    free(scale);
}
示例#27
0
void test_aggregate(int dryrun) {
  
    int i, j, rc, bytes, elems[2] = {MAXPROC, MAXELEMS};
    double *ddst_put[MAXPROC];
    double *ddst_get[MAXPROC];
    double *dsrc[MAXPROC];
    armci_hdl_t aggr_hdl_put[MAXPROC];
    armci_hdl_t aggr_hdl_get[MAXPROC];
    armci_hdl_t hdl_put[MAXELEMS];
    armci_hdl_t hdl_get[MAXELEMS];
    armci_giov_t darr;
    void *src_ptr[MAX_REQUESTS], *dst_ptr[MAX_REQUESTS];
    int start = 0, end = 0;
    double start_time;
        
    create_array((void**)ddst_put, sizeof(double),2, elems);
    create_array((void**)ddst_get, sizeof(double),2, elems);
    create_array((void**)dsrc, sizeof(double),1, &elems[1]);
    
    for(i=0; i<elems[1]; i++) dsrc[me][i]=i*1.001*(me+1);
    for(i=0; i<elems[0]*elems[1]; i++) {
      ddst_put[me][i]=0.0;
      ddst_get[me][i]=0.0;
    }
    
    MP_BARRIER();

    /* only proc 0 does the work */
    if(me == 0) {
      if(!dryrun)printf("Transferring %d doubles (Not an array of %d doubles)\n", MAXELEMS, MAXELEMS);
      
      /* initializing non-blocking handles */
      for(i=0; i<elems[1]; i++) ARMCI_INIT_HANDLE(&hdl_put[i]);
      for(i=0; i<elems[1]; i++) ARMCI_INIT_HANDLE(&hdl_get[i]);
      
      /* aggregate handles */
      for(i=0; i<nproc; i++) ARMCI_INIT_HANDLE(&aggr_hdl_put[i]);
      for(i=0; i<nproc; i++) ARMCI_INIT_HANDLE(&aggr_hdl_get[i]);
      for(i=0; i<nproc; i++) ARMCI_SET_AGGREGATE_HANDLE(&aggr_hdl_put[i]);
      for(i=0; i<nproc; i++) ARMCI_SET_AGGREGATE_HANDLE(&aggr_hdl_get[i]);    
      
      bytes = sizeof(double);
      
      /* **************** PUT **************** */    
      /* register put */
      start_time=MP_TIMER();
      start = 0; end = elems[1]; 
      for(i=1; i<nproc; i++) {
	for(j=start; j<end; j++) {  
	  ARMCI_NbPutValueDouble(dsrc[me][j], &ddst_put[i][me*elems[1]+j], i, 
				 &hdl_put[j]);
	}
	for(j=start; j<end; j++) ARMCI_Wait(&hdl_put[j]);
      }
      if(!dryrun)printf("%d: Value Put time      = %.2es\n", me, MP_TIMER()-start_time);
 
      /* vector put */
      start_time=MP_TIMER();
      for(i=1; i<nproc; i++) {
	for(j=start; j<end; j++) {
	  src_ptr[j] = (void *)&dsrc[me][j];
	  dst_ptr[j] = (void *)&ddst_put[i][me*elems[1]+j];
	}
	darr.src_ptr_array = src_ptr;
	darr.dst_ptr_array = dst_ptr;
	darr.bytes = sizeof(double);
	darr.ptr_array_len = elems[1];
	if((rc=ARMCI_NbPutV(&darr, 1, i, &hdl_put[i])))
	  ARMCI_Error("armci_nbputv failed\n",rc);
      }
      for(i=1; i<nproc; i++) ARMCI_Wait(&hdl_put[i]);
      if(!dryrun)printf("%d: Vector Put time     = %.2es\n", me, MP_TIMER()-start_time);
      
      /* regular put */
      start_time=MP_TIMER();    
      for(i=1; i<nproc; i++) {
	for(j=start; j<end; j++) {  
	  if((rc=ARMCI_NbPut(&dsrc[me][j], &ddst_put[i][me*elems[1]+j], bytes,
			     i, &hdl_put[j])))
	    ARMCI_Error("armci_nbput failed\n",rc);
	}
	for(j=start; j<end; j++) ARMCI_Wait(&hdl_put[j]);
      }
      if(!dryrun)printf("%d: Regular Put time    = %.2es\n", me, MP_TIMER()-start_time);
      
      /* aggregate put */
      start_time=MP_TIMER();
      for(i=1; i<nproc; i++) {
	for(j=start; j<end; j++) {  
	  if((rc=ARMCI_NbPut(&dsrc[me][j], &ddst_put[i][me*elems[1]+j], bytes,
			     i,  &aggr_hdl_put[i])))
	    ARMCI_Error("armci_nbput failed\n",rc);
	}
      }
      for(i=1; i<nproc; i++) ARMCI_Wait(&aggr_hdl_put[i]);
      if(!dryrun)printf("%d: Aggregate Put time  = %.2es\n\n", me, MP_TIMER()-start_time);
      
      
      /* **************** GET **************** */    
      
      /* vector get */
      start_time=MP_TIMER();
      for(i=1; i<nproc; i++) {
	for(j=start; j<end; j++) {
	  src_ptr[j] = (void *)&dsrc[i][j];
	  dst_ptr[j] = (void *)&ddst_get[me][i*elems[1]+j];
	}
	darr.src_ptr_array = src_ptr;
	darr.dst_ptr_array = dst_ptr;
	darr.bytes = sizeof(double);
	darr.ptr_array_len = elems[1];
	if((rc=ARMCI_NbGetV(&darr, 1, i, &hdl_get[i])))
	  ARMCI_Error("armci_nbgetv failed\n",rc);
	ARMCI_Wait(&hdl_get[i]);
      }
      if(!dryrun)printf("%d: Vector Get time     = %.2es\n", me, MP_TIMER()-start_time);
      
      /* regular get */
      start_time=MP_TIMER();    
      for(i=1; i<nproc; i++) {
	for(j=start; j<end; j++) {  
	  if((rc=ARMCI_NbGet(&dsrc[i][j], &ddst_get[me][i*elems[1]+j], bytes,
			     i, &hdl_get[j])))
	    ARMCI_Error("armci_nbget failed\n",rc);
	}
	for(j=start; j<end; j++) ARMCI_Wait(&hdl_get[j]);
      }
      if(!dryrun)printf("%d: Regular Get time    = %.2es\n", me, MP_TIMER()-start_time);
      
      /* aggregate get */
      start_time=MP_TIMER();
      for(i=1; i<nproc; i++) {
	for(j=start; j<end; j++) {  
	  ARMCI_NbGet(&dsrc[i][j], &ddst_get[me][i*elems[1]+j], bytes,
		      i, &aggr_hdl_get[i]);
	}
      }
      for(i=1; i<nproc; i++) ARMCI_Wait(&aggr_hdl_get[i]);
      if(!dryrun)printf("%d: Aggregate Get time  = %.2es\n", me, MP_TIMER()-start_time);
    }

    MP_BARRIER();
    ARMCI_AllFence();
    MP_BARRIER();

    /* Verify */
    if(!(me==0))
      for(j=0; j<elems[1]; j++) {
	if( ARMCI_ABS(ddst_put[me][j]-j*1.001) > 0.1) {
	  ARMCI_Error("aggregate put failed...1", 0);
	}
      }
    MP_BARRIER();
    if(!dryrun)if(me==0) printf("\n  aggregate put ..O.K.\n"); fflush(stdout);

    if(me==0) {
      for(i=1; i<nproc; i++) {
	for(j=0; j<elems[1]; j++) {
	  if( ARMCI_ABS(ddst_get[me][i*elems[1]+j]-j*1.001*(i+1)) > 0.1) {
	    ARMCI_Error("aggregate get failed...1", 0);
	  }
	}
      }
    }
    MP_BARRIER();
    if(!dryrun)if(me==0) printf("  aggregate get ..O.K.\n"); fflush(stdout);


    ARMCI_AllFence();
    MP_BARRIER();
    
    if(!dryrun)if(me==0){printf("O.K.\n"); fflush(stdout);}
    destroy_array((void **)ddst_put);
    destroy_array((void **)ddst_get);
    destroy_array((void **)dsrc);
}
示例#28
0
文件: test.c 项目: Sea94/array
int main()
{
//Тест #1: создание массива, вставка элементов, их получение, удаление массива
    ARRAY array = create_array();                       assert(array != NULL);   //Создание массива
    int el = 1;                        
    int result = insert(array,0,&el);                   assert(result == 0);     //Вставка элемента el в array[0]
    int * res_el;
    res_el = get(array,0);                              assert(res_el != NULL);  //"Вытаскивание" элемента 0 из массива. Если элемента нет, это хреново
                                                        assert(el == *res_el);   //"Вытаскивание" чего-то другого - тоже не айс
    result = destroy_array(array);                      assert(result != -1);
    result = destroy_array(array);                      assert(result == -1);    //Должно возвращаться -1. Мы уже почистили этот массив. 
    array = create_array();                             assert(array != NULL);   //Массив должен создаться без проблем
    //Все то же самое, что и выше - должно работать нормально
    el = 10;
    result = insert(array,1,&el);                       assert(result == 0);     //Вставка элемента el в array[1]
    res_el = get(array,1);                              assert(res_el != NULL);  //"Вытаскивание" элемента 0 из массива. Если элемента нет, это хреново
                                                        assert(el == *res_el);   //"Вытаскивание" чего-то другого - тоже не айс
    result = destroy_array(array);                      assert(result != -1);
    result = destroy_array(array);                      assert(result == -1);    //Должно возвращаться -1. Мы уже почистили этот массив. 
    array = create_array();                             assert(array != NULL); 
    result = insert(array,-1,&el);                      assert(result != 0);     //Ожидается ошибка. Нет отрицательных индексов.
    res_el = get(array,-1);                             assert(res_el == NULL);  //То же
    result = destroy_array(array);                      assert(result != 0);     //Ожидается ошибка. В массив мы ничего не положили.
//Тест #2: сортировка массива. Задача: ввести массив, отсортировать и убедиться, что он отсортирован.
    array = create_array();                             assert(array != NULL); 
    srand(time(NULL));
    int max_index = -1,i;
    float num[100];
    for (i = 0; i < 100; i++)
    {
        num[i] = rand() % 100;
        //printf("%f\n",num[i]);
        //printf("%d\n",i);
        result = insert(array,i,&num[i]);               assert(result == 0);
        //printf("%d\n",i);
        if (max_index < i) max_index = i;
    }
    float * element;
    //Теперь сортировка. Без извращений. Просто пузырьком.
    float * prev, * cur, * tmp;
    int j;
    for (i = 0; i < 100; i++)
    {
        for (j = 1; j < 100; j++)
        {
            cur = get(array,j);                         assert(cur != NULL);
            prev = get(array,j-1);                      assert(prev != NULL);
            if (* prev > * cur)
            {
               result = insert(array, j - 1, cur);      assert(result == 0);
               insert(array, j, prev);                  assert(result == 0);
            }
        }
    }
    for (i = 1; i < 100; i++)
    {
        prev = get(array,i-1);                          assert(prev != NULL);
        element = get(array,i);                         assert(element != NULL);
                                                        assert(*prev <= *element);   //Если это условие не выполнено, то массив не был нормально отсортирован.
    }
    result = destroy_array(array);                      assert(result != -1);
}
示例#29
0
/**
 * Reads file.
 *
 * @param p0 the array
 * @param p1 the array count
 * @param p2 the array size
 * @param p3 the file name
 * @param p4 the file name count
 */
void read_file(void* p0, void* p1, void* p2, const void* p3, const void* p4) {

    if (p4 != NULL_POINTER) {

        int* nc = (int*) p4;

        if (p3 != NULL_POINTER) {

            void** n = (void**) p3;

            if (p2 != NULL_POINTER) {

                int* as = (int*) p2;

                if (p1 != NULL_POINTER) {

                    int* ac = (int*) p1;

                    // Initialize terminated file name and its size.
                    char* tn = CHARACTER_NULL_POINTER;
                    int tns = *nc + 1;

                    // Create terminated file name.
                    create_array((void*) &tn, (void*) &CHARACTER_ARRAY, (void*) &tns);

                    // Initialize destination array index.
                    int i = 0;

                    // Set terminated file name by first copying the actual name and then
                    // adding the null termination character.
                    set_array_elements((void*) &tn, (void*) &CHARACTER_ARRAY, (void*) &i, p3, p4);
                    set_array_element((void*) &tn, (void*) &CHARACTER_ARRAY, p4, (void*) &NULL_CHARACTER);

    fprintf(stderr, "read file name: %s\n", tn);
    fprintf(stderr, "read file name size: %i\n", tns);

                    // Open file.
                    // CAUTION! The file name cannot be handed over as is.
                    // CYBOI strings are NOT terminated with the null character '\0'.
                    // Since 'fopen' expects a null terminated string, the termination character
                    // must be added to the string before that is used to open the file.
                    FILE* f = fopen(tn, "r");

                    if (f != NULL_POINTER) {

                        // Read first character.
                        char c = fgetc(f);

                        while (1) {

                            if (c == EOF) {

                                break;
                            }

                            if (*ac == *as) {

                                // Increase size.
                                *as = *as * FILE_RESIZE_FACTOR + 1;

                                // Resize array.
                                resize_array(p0, (void*) &CHARACTER_ARRAY, p2);
                            }

                            if (*ac < *as) {

                                // Set character in destination array.
                                // The array count serves as index for setting the character.
                                set_array_element(p0, (void*) &CHARACTER_ARRAY, p1, (void*) &c);

                                // Increase array count.
                                (*ac)++;

                            } else {

//??                                log_message((void*) &ERROR_LOG_LEVEL, (void*) &"Could not read file. The index exceeds the array size.");
                            }

                            // Read next character.
                            c = fgetc(f);
                        }

                        // Close file.
                        fclose(f);

                        // Destroy terminated file name.
                        destroy_array((void*) &tn, (void*) &CHARACTER_ARRAY, (void*) &tns);

                    } else {

//??                        log_message((void*) &ERROR_LOG_LEVEL, (void*) &"Could not read file. The file is null.");
                    }

                } else {

//??                    log_message((void*) &ERROR_LOG_LEVEL, (void*) &"Could not read file. The array count is null.");
                }

            } else {

//??                log_message((void*) &ERROR_LOG_LEVEL, (void*) &"Could not read file. The array size is null.");
            }

        } else {

//??            log_message((void*) &ERROR_LOG_LEVEL, (void*) &"Could not read file. The file name is null.");
        }

    } else {

//??        log_message((void*) &ERROR_LOG_LEVEL, (void*) &"Could not read file. The file name count is null.");
    }
}
示例#30
0
void test_notify(int ndim)
{
  int lo[MAXDIMS], hi[MAXDIMS], count[MAXDIMS];
  int stride[MAXDIMS];
  int dim, elems;
  int i, Idx = 1, idx = 0;
  void *b[MAXPROC], *a[MAXPROC];
  int left = (me + nproc - 1) % nproc;
  int right = (me + 1) % nproc;
  int loopcnt = 1, less = 2, strl; /* less>1 takes a partial plane */


  /* create shared and local arrays */
  create_array(b, sizeof(double), ndim, dimsB);
  create_array(a, sizeof(double), ndim, dimsB);

  elems = get_elems(ndim, stride, dimsB, sizeof(double));
  init((double *)a[me], ndim, elems, dimsB);

  for (i = 0; i < ndim; i++) {
    lo[i] = 0;
    hi[i] = (less > dimsB[i]) ? dimsB[i] - 1 : dimsB[i] - less;
    count[i] = hi[i] - lo[i] + 1;
  }
  count[0] *= sizeof(double);

  for (i = 0; i < ndim - 1; i++) {
    Idx *= dimsB[i];
  }

  ARMCI_Barrier();
  if (me == 0) {
    printf("--------array[%d", dimsB[0]);
    for (dim = 1; dim < ndim; dim++) {
      printf(",%d", dimsB[dim]);
    }
    printf("]--------\n");
    fflush(stdout);
  }

  ARMCI_Barrier();
  loopcnt = (ndim > 1) ? dimsB[ndim-1] : 1;
  strl    = (ndim > 1) ? ndim - 2 : 0; /* strides of the subpatch to transfer */

  for (i = 0; i < loopcnt; i++) {
    int wc;

    if (me == 0) {

      ARMCI_PutS((double *)a[me] + idx, stride,
                 (double *)b[left] + idx, stride, count, strl, left);
#if DEBUG_
      printf("%d-%d: ps=%p pd=%p i=%d idx=%d count=%d\n", me, left, (double *)
             a[me] + idx, (double *)b[left] + idx, i, idx, count[0]);
      fflush(stdout);
#endif
      (void)armci_notify(left);
      (void)armci_notify_wait(right, &wc);

    }
    else {


      (void)armci_notify_wait(right, &wc);
      ARMCI_PutS((double *)b[me] + idx, stride,
                 (double *)b[left] + idx, stride, count, strl, left);
#if DEBUG_
      printf("%d: ps=%p pd=%p i=%d idx=%d count=%d\n", me, (double *)b[me] + idx,
             (double *)b[left] + idx, i, idx, count[0]);
      fflush(stdout);
#endif
      (void)armci_notify(left);
    }

    idx += Idx; /* advance to the next slab */
  }

  ARMCI_Barrier();

  if (me == 0) {
    compare_patches(0., ndim, (double *)a[0], lo, hi, dimsB,
                    (double *)b[0], lo, hi, dimsB);
    printf("OK\n");
  }

  ARMCI_Barrier();
  destroy_array(b);
  destroy_array(a);
}