X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/b3a6228d1e65a6f7eea5b97ae0fa2668af06d59d..0d17d68ef25921dbdfde20b85e0a1de773ca263e:/testsuite/surf/lmm_usage.c diff --git a/testsuite/surf/lmm_usage.c b/testsuite/surf/lmm_usage.c index 346d073a32..586cd8c902 100644 --- a/testsuite/surf/lmm_usage.c +++ b/testsuite/surf/lmm_usage.c @@ -1,8 +1,7 @@ -/* $Id$ */ - /* A few tests for the maxmin library */ -/* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */ +/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team. + * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -18,10 +17,10 @@ #include XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, - "Messages specific for surf example"); + "Messages specific for surf example"); -#define PRINT_VAR(var) DEBUG1(#var " = %g",lmm_variable_getvalue(var)); -#define SHOW_EXPR(expr) DEBUG1(#expr " = %g",expr); +#define PRINT_VAR(var) XBT_DEBUG(#var " = %g",lmm_variable_getvalue(var)); +#define SHOW_EXPR(expr) XBT_DEBUG(#expr " = %g",expr); /* */ /* ______ */ @@ -31,13 +30,12 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test, typedef enum { MAXMIN, - SDP, LAGRANGE_RENO, LAGRANGE_VEGAS, } method_t; static double dichotomy(double func(double), double min, double max, - double min_error) + double min_error) { double middle; double min_func, max_func, middle_func; @@ -54,26 +52,36 @@ static double dichotomy(double func(double), double min, double max, if ((min_func > 0 && max_func < 0)) abort(); + SHOW_EXPR(min_error); + while (overall_error > min_error) { + SHOW_EXPR(overall_error); if ((min_func > 0 && max_func > 0) || - (min_func < 0 && max_func < 0) || (min_func > 0 && max_func < 0)) { + (min_func < 0 && max_func < 0) || (min_func > 0 && max_func < 0)) { abort(); } + SHOW_EXPR(min); + SHOW_EXPR(min_func); + SHOW_EXPR(max); + SHOW_EXPR(max_func); + middle = (max + min) / 2.0; if ((min == middle) || (max == middle)) { break; } middle_func = func(middle); + SHOW_EXPR(middle); + SHOW_EXPR(middle_func); if (middle_func < 0) { min = middle; min_func = middle_func; - overall_error = max - middle_func; + overall_error = max_func - middle_func; } else if (middle_func > 0) { max = middle; max_func = middle_func; - overall_error = max - middle_func; + overall_error = middle_func - min_func; } else { overall_error = 0; } @@ -86,10 +94,10 @@ double b_test_1 = 0; static double diff_lagrange_test_1(double x) { return -(3 / (1 + 3 * x * x / 2) - - 3 / (2 * (3 * (a_test_1 - x) * (a_test_1 - x) / 2 + 1)) + - 3 / (2 * - (3 * (b_test_1 - a_test_1 + x) * - (b_test_1 - a_test_1 + x) / 2 + 1))); + 3 / (2 * (3 * (a_test_1 - x) * (a_test_1 - x) / 2 + 1)) + + 3 / (2 * + (3 * (b_test_1 - a_test_1 + x) * + (b_test_1 - a_test_1 + x) / 2 + 1))); } void test1(method_t method); @@ -109,10 +117,10 @@ void test1(method_t method) if (method == LAGRANGE_VEGAS) lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, - func_vegas_fpi); + func_vegas_fpi); else if (method == LAGRANGE_RENO) lmm_set_default_protocol_function(func_reno_f, func_reno_fpi, - func_reno_fpi); + func_reno_fpi); Sys = lmm_system_new(); L1 = lmm_constraint_new(Sys, (void *) "L1", a); @@ -124,10 +132,10 @@ void test1(method_t method) R_2 = lmm_variable_new(Sys, (void *) "R 2", 1.0, -1.0, 1); R_3 = lmm_variable_new(Sys, (void *) "R 3", 1.0, -1.0, 1); - lmm_update_variable_latency(Sys, R_1_2_3, 1.0); - lmm_update_variable_latency(Sys, R_1, 1.0); - lmm_update_variable_latency(Sys, R_2, 1.0); - lmm_update_variable_latency(Sys, R_3, 1.0); + lmm_update_variable_weight(Sys, R_1_2_3, 1.0); + lmm_update_variable_weight(Sys, R_1, 1.0); + lmm_update_variable_weight(Sys, R_2, 1.0); + lmm_update_variable_weight(Sys, R_3, 1.0); lmm_expand(Sys, L1, R_1_2_3, 1.0); lmm_expand(Sys, L2, R_1_2_3, 1.0); @@ -142,13 +150,9 @@ void test1(method_t method) if (method == MAXMIN) { lmm_solve(Sys); -#ifdef HAVE_SDP - } else if (method == SDP) { - sdp_solve(Sys); -#endif } else if (method == LAGRANGE_VEGAS) { double x = 3 * a / 4 - 3 * b / 8 + - sqrt(9 * b * b + 4 * a * a - 4 * a * b) / 8; + sqrt(9 * b * b + 4 * a * a - 4 * a * b) / 8; /* Computed with mupad and D_f=1.0 */ double max_deviation = 0.0; if (x > a) { @@ -161,25 +165,25 @@ void test1(method_t method) lagrange_solve(Sys); max_deviation = - MAX(max_deviation, fabs(lmm_variable_getvalue(R_1) - x)); + MAX(max_deviation, fabs(lmm_variable_getvalue(R_1) - x)); max_deviation = - MAX(max_deviation, fabs(lmm_variable_getvalue(R_3) - x)); + MAX(max_deviation, fabs(lmm_variable_getvalue(R_3) - x)); max_deviation = - MAX(max_deviation, fabs(lmm_variable_getvalue(R_2) - (b - a + x))); + MAX(max_deviation, fabs(lmm_variable_getvalue(R_2) - (b - a + x))); max_deviation = - MAX(max_deviation, fabs(lmm_variable_getvalue(R_1_2_3) - (a - x))); + MAX(max_deviation, fabs(lmm_variable_getvalue(R_1_2_3) - (a - x))); if (max_deviation > MAXMIN_PRECISION) { - WARN1("Max Deviation from optimal solution : %g", max_deviation); - WARN1("Found x = %1.20f", x); - WARN2("Deviation from optimal solution (R_1 = %g): %1.20f", x, - lmm_variable_getvalue(R_1) - x); - WARN2("Deviation from optimal solution (R_2 = %g): %1.20f", - b - a + x, lmm_variable_getvalue(R_2) - (b - a + x)); - WARN2("Deviation from optimal solution (R_3 = %g): %1.20f", x, - lmm_variable_getvalue(R_3) - x); - WARN2("Deviation from optimal solution (R_1_2_3 = %g): %1.20f", - a - x, lmm_variable_getvalue(R_1_2_3) - (a - x)); + XBT_WARN("Max Deviation from optimal solution : %g", max_deviation); + XBT_WARN("Found x = %1.20f", x); + XBT_WARN("Deviation from optimal solution (R_1 = %g): %1.20f", x, + lmm_variable_getvalue(R_1) - x); + XBT_WARN("Deviation from optimal solution (R_2 = %g): %1.20f", + b - a + x, lmm_variable_getvalue(R_2) - (b - a + x)); + XBT_WARN("Deviation from optimal solution (R_3 = %g): %1.20f", x, + lmm_variable_getvalue(R_3) - x); + XBT_WARN("Deviation from optimal solution (R_1_2_3 = %g): %1.20f", + a - x, lmm_variable_getvalue(R_1_2_3) - (a - x)); } } else if (method == LAGRANGE_RENO) { double x; @@ -187,7 +191,7 @@ void test1(method_t method) a_test_1 = a; b_test_1 = b; - x = dichotomy(diff_lagrange_test_1, 0, a, 1e-8); + x = dichotomy(diff_lagrange_test_1, 0, a, 1e-13); if (x < 0) x = 0; @@ -196,28 +200,28 @@ void test1(method_t method) lagrange_solve(Sys); max_deviation = - MAX(max_deviation, fabs(lmm_variable_getvalue(R_1) - x)); + MAX(max_deviation, fabs(lmm_variable_getvalue(R_1) - x)); max_deviation = - MAX(max_deviation, fabs(lmm_variable_getvalue(R_3) - x)); + MAX(max_deviation, fabs(lmm_variable_getvalue(R_3) - x)); max_deviation = - MAX(max_deviation, fabs(lmm_variable_getvalue(R_2) - (b - a + x))); + MAX(max_deviation, fabs(lmm_variable_getvalue(R_2) - (b - a + x))); max_deviation = - MAX(max_deviation, fabs(lmm_variable_getvalue(R_1_2_3) - (a - x))); + MAX(max_deviation, fabs(lmm_variable_getvalue(R_1_2_3) - (a - x))); if (max_deviation > MAXMIN_PRECISION) { - WARN1("Max Deviation from optimal solution : %g", max_deviation); - WARN1("Found x = %1.20f", x); - WARN2("Deviation from optimal solution (R_1 = %g): %1.20f", x, - lmm_variable_getvalue(R_1) - x); - WARN2("Deviation from optimal solution (R_2 = %g): %1.20f", - b - a + x, lmm_variable_getvalue(R_2) - (b - a + x)); - WARN2("Deviation from optimal solution (R_3 = %g): %1.20f", x, - lmm_variable_getvalue(R_3) - x); - WARN2("Deviation from optimal solution (R_1_2_3 = %g): %1.20f", - a - x, lmm_variable_getvalue(R_1_2_3) - (a - x)); + XBT_WARN("Max Deviation from optimal solution : %g", max_deviation); + XBT_WARN("Found x = %1.20f", x); + XBT_WARN("Deviation from optimal solution (R_1 = %g): %1.20f", x, + lmm_variable_getvalue(R_1) - x); + XBT_WARN("Deviation from optimal solution (R_2 = %g): %1.20f", + b - a + x, lmm_variable_getvalue(R_2) - (b - a + x)); + XBT_WARN("Deviation from optimal solution (R_3 = %g): %1.20f", x, + lmm_variable_getvalue(R_3) - x); + XBT_WARN("Deviation from optimal solution (R_1_2_3 = %g): %1.20f", + a - x, lmm_variable_getvalue(R_1_2_3) - (a - x)); } } else { - xbt_assert0(0, "Invalid method"); + xbt_die( "Invalid method"); } PRINT_VAR(R_1_2_3); @@ -241,10 +245,10 @@ void test2(method_t method) if (method == LAGRANGE_VEGAS) lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, - func_vegas_fpi); + func_vegas_fpi); else if (method == LAGRANGE_RENO) lmm_set_default_protocol_function(func_reno_f, func_reno_fp, - func_reno_fpi); + func_reno_fpi); Sys = lmm_system_new(); CPU1 = lmm_constraint_new(Sys, (void *) "CPU1", 200.0); @@ -253,8 +257,8 @@ void test2(method_t method) T1 = lmm_variable_new(Sys, (void *) "T1", 1.0, -1.0, 1); T2 = lmm_variable_new(Sys, (void *) "T2", 1.0, -1.0, 1); - lmm_update_variable_latency(Sys, T1, 1.0); - lmm_update_variable_latency(Sys, T2, 1.0); + lmm_update_variable_weight(Sys, T1, 1.0); + lmm_update_variable_weight(Sys, T2, 1.0); lmm_expand(Sys, CPU1, T1, 1.0); @@ -264,16 +268,12 @@ void test2(method_t method) if (method == MAXMIN) { lmm_solve(Sys); -#ifdef HAVE_SDP - } else if (method == SDP) { - sdp_solve(Sys); -#endif } else if (method == LAGRANGE_VEGAS) { lagrange_solve(Sys); } else if (method == LAGRANGE_RENO) { lagrange_solve(Sys); } else { - xbt_assert0(0, "Invalid method"); + xbt_die("Invalid method"); } PRINT_VAR(T1); @@ -306,9 +306,8 @@ void test3(method_t method) 1, 1, 1, 1, 1 }; - - /*A = xbt_new0(double*, links + 5);*/ - A = xbt_new0(double*, links + 5); + + A = xbt_new0(double *, links + 5); for (i = 0; i < links + 5; i++) { A[i] = xbt_new0(double, flows + 5); @@ -316,7 +315,7 @@ void test3(method_t method) A[i][j] = 0.0; if (i >= links || j >= flows) { - A[i][j] = 0.0; + A[i][j] = 0.0; } } } @@ -393,21 +392,21 @@ void test3(method_t method) if (method == LAGRANGE_VEGAS) lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp, - func_vegas_fpi); + func_vegas_fpi); else if (method == LAGRANGE_RENO) lmm_set_default_protocol_function(func_reno_f, func_reno_fp, - func_reno_fpi); + func_reno_fpi); Sys = lmm_system_new(); - tmp_name = xbt_new0(char *,31); + tmp_name = xbt_new0(char *, 31); /* * Creates the constraints */ - tmp_cnst = xbt_new0(lmm_constraint_t,15); + tmp_cnst = xbt_new0(lmm_constraint_t, 15); for (i = 0; i < 15; i++) { tmp_name[i] = bprintf("C_%03d", i); tmp_cnst[i] = lmm_constraint_new(Sys, (void *) tmp_name[i], B[i]); @@ -417,12 +416,12 @@ void test3(method_t method) /* * Creates the variables */ - tmp_var = xbt_new0(lmm_variable_t,16); + tmp_var = xbt_new0(lmm_variable_t, 16); for (j = 0; j < 16; j++) { tmp_name[i + j] = bprintf("X_%03d", j); tmp_var[j] = - lmm_variable_new(Sys, (void *) tmp_name[i + j], 1.0, -1.0, 15); - lmm_update_variable_latency(Sys, tmp_var[j], 1.0); + lmm_variable_new(Sys, (void *) tmp_name[i + j], 1.0, -1.0, 15); + lmm_update_variable_weight(Sys, tmp_var[j], 1.0); } /* @@ -431,7 +430,7 @@ void test3(method_t method) for (i = 0; i < 15; i++) { for (j = 0; j < 16; j++) { if (A[i][j]) { - lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0); + lmm_expand(Sys, tmp_cnst[i], tmp_var[j], 1.0); } } } @@ -440,26 +439,27 @@ void test3(method_t method) if (method == MAXMIN) { lmm_solve(Sys); -#ifdef HAVE_SDP - } else if (method == SDP) { - sdp_solve(Sys); -#endif } else if (method == LAGRANGE_VEGAS) { lagrange_solve(Sys); } else if (method == LAGRANGE_RENO) { lagrange_solve(Sys); } else { - xbt_assert0(0, "Invalid method"); + xbt_die("Invalid method"); } for (j = 0; j < 16; j++) { PRINT_VAR(tmp_var[j]); } - free(tmp_var); - free(tmp_cnst); - free(tmp_name); + xbt_free(tmp_var); + xbt_free(tmp_cnst); + for (i = 0; i < 31; i++) + xbt_free(tmp_name[i]); + xbt_free(tmp_name); lmm_system_free(Sys); + for (i = 0; i < links + 5; i++) + xbt_free(A[i]); + xbt_free(A); } #ifdef __BORLANDC__ @@ -470,40 +470,28 @@ int main(int argc, char **argv) { xbt_init(&argc, argv); - INFO0("***** Test 1 (Max-Min)"); + XBT_INFO("***** Test 1 (Max-Min)"); test1(MAXMIN); -#ifdef HAVE_SDP - INFO0("***** Test 1 (SDP)"); - test1(SDP); -#endif - INFO0("***** Test 1 (Lagrange - Vegas)"); + XBT_INFO("***** Test 1 (Lagrange - Vegas)"); test1(LAGRANGE_VEGAS); - INFO0("***** Test 1 (Lagrange - Reno)"); + XBT_INFO("***** Test 1 (Lagrange - Reno)"); test1(LAGRANGE_RENO); - - - INFO0("***** Test 2 (Max-Min)"); + + + XBT_INFO("***** Test 2 (Max-Min)"); test2(MAXMIN); -#ifdef HAVE_SDP - INFO0("***** Test 2 (SDP)"); - test2(SDP); -#endif - INFO0("***** Test 2 (Lagrange - Vegas)"); + XBT_INFO("***** Test 2 (Lagrange - Vegas)"); test2(LAGRANGE_VEGAS); - INFO0("***** Test 2 (Lagrange - Reno)"); + XBT_INFO("***** Test 2 (Lagrange - Reno)"); test2(LAGRANGE_RENO); - INFO0("***** Test 3 (Max-Min)"); + XBT_INFO("***** Test 3 (Max-Min)"); test3(MAXMIN); -#ifdef HAVE_SDP - INFO0("***** Test 3 (SDP)"); - test3(SDP); -#endif - INFO0("***** Test 3 (Lagrange - Vegas)"); + XBT_INFO("***** Test 3 (Lagrange - Vegas)"); test3(LAGRANGE_VEGAS); - INFO0("***** Test 3 (Lagrange - Reno)"); + XBT_INFO("***** Test 3 (Lagrange - Reno)"); test3(LAGRANGE_RENO); return 0;