예제 #1
0
/*

gcc -o para4 parallel-memalloc.c  -lgsl -lgslcblas -fopenmp -O3


*/
int main(){
	const int N=1000000;
	int i, k;
	time_t time1, time2;
	struct foo **x = (struct foo **) malloc(N*sizeof(struct foo *));

	/* SERIAL CODE */
	printf("\nAllocating memory, serial code");
	time(&time1);
	for(i=0;i<N;i++){
		x[i] = create_foo();
	}
	time(&time2);

	printf("\nthe operation took %d seconds\n", (int) (time2-time1));


	printf("\nFreeing memory, serial code");
	time(&time1);
	for(i=0;i<N;i++){
		free_foo(x[i]);
	}
	time(&time2);

	printf("\nthe operation took %d seconds\n", (int) (time2-time1));



	/* PARALLEL CODE */
	printf("\nAllocating memory, parallel code");
	time(&time1);
#pragma omp parallel for schedule(static,100000)
	for(i=0;i<N;i++){
		x[i] = create_foo();
	}
	time(&time2);

	printf("\nthe operation took %d seconds\n", (int) (time2-time1));


	printf("\nFreeing memory, parallel code");
	time(&time1);
#pragma omp parallel for
	for(i=0;i<N;i++){
		free_foo(x[i]);
	}
	time(&time2);

	printf("\nthe operation took %d seconds\n", (int) (time2-time1));

	return 0;
}
예제 #2
0
int main() {
    unsigned long long i;

    list = (struct foo**)malloc(N*sizeof(struct foo));
    DYNAMIC_INIT(foo);

    for (i = 0; i < N; i++) {
	unsigned long long j;
	list[i] = TEST_ALLOC(foo);
	for (j = 0; j < i; j++)
	    if (list[j] == list[i]) {
		fprintf(stderr, "allocated the same chunk twice: %p\n", list[i]);
	    }
	
	list[i]->free = 0;
	list[i]->n = i;
	list[i]->moved = 0;
    }

    for (i = 0; i + 10 < N; i+=10) {
	int j;
	/* free 8 out of 10 */
	for (j = 0; j < 8; j++) {
	    free_foo(i+j);
	}
    }

    check_list();

#ifdef TEST_DEFRAGMENT
    fprintf(stderr, "density before defragment: %f\n", TEST_DENSITY(foo));
    TEST_DEFRAGMENT(foo, relocate, NULL);
    fprintf(stderr, "density after defragment:  %f\n", TEST_DENSITY(foo));
#endif

    check_list();

    for (i = 0; i < N; i++) {
	if (list[i]) free_foo(i);
    }

    TEST_DEINIT(foo);
    free(list);

    return 0;
}