gs_smart_mapping_matrix_func.c

Go to the documentation of this file.
00001 /*
00002  *  @file
00003  *
00004  *  This file contains functions for manipulating
00005  *  the task to server mapping matrix and 
00006  *  task to server mapping vector which are used to generate
00007  *  the mapping solutions.
00008  */
00009 
00010 
00011 
00012 
00013 #include "config.h"
00014 
00015 #ifdef GS_SMART_GRIDSOLVE
00016 #include "gs_smart_task_graph.h"
00017 #include "gs_smart_mapping_graph.h"
00018 
00019 
00020 /*
00021  * This function is used to iterate through every possible task to server 
00022  * assignment.  The input mapping_matrix is a binary matrix which records 
00023  * the assignment of tasks to servers.  This function increments the 
00024  * mapping_matrix to the next task-to-server assignment.
00025  *
00026  * @param mapping_matrix -- binary matrix which represent task to server assignemnt
00027  * @param number -- the current solution number
00028  * @param base - number of servers
00029  * @param nb_tasks -- number of tasks
00030  *
00031  */
00032 
00033 
00034 
00035 int gs_smart_next_permutation(int ** mapping_matrix, int number, int base, int nb_tasks){
00036   int mod;
00037   int multiple=number;
00038   int fin=0;
00039   int col_nb=0;
00040 
00041   if(!mapping_matrix){
00042     ERRPRINTF("SMART: Error could not allocate memory to mapping matrix\n");
00043     return -1;
00044   }
00045 
00046   while(col_nb<nb_tasks){
00047     if(multiple>=base&&col_nb<nb_tasks){
00048       mod = (multiple%base);
00049       mapping_matrix[mod][col_nb]=1;
00050       multiple=(int)multiple/base;
00051       col_nb++;
00052     }
00053     if(fin==1&&col_nb<nb_tasks){
00054      mapping_matrix[0][col_nb]=1;
00055      col_nb++;
00056     }
00057     else{
00058       if(multiple<base&&col_nb<nb_tasks){
00059         mapping_matrix[multiple][col_nb]=1;
00060         col_nb++;
00061         fin=1;
00062       }
00063     }
00064   }
00065   return 0;
00066 }
00067 
00068 
00069 
00070 
00071 
00072 
00073 /*
00074  * This function resets/zeros the input binary mapping_matrix
00075  *
00076  * @param mapping_matrix -- binary matrix which represent task to server assignemnt
00077  * @param nb_servers - number of servers
00078  * @param nb_tasks -- number of tasks
00079  *
00080  */
00081 
00082 
00083 int gs_smart_zero_mapping_matrix(int **mapping_matrix, int nb_servers,  int nb_tasks)
00084 {
00085    int i;
00086    if(!mapping_matrix)  return -1;
00087 
00088    if((nb_tasks<=0)||(nb_servers<=0)){
00089      ERRPRINTF("SMART: Number of tasks or servers is not greater zero\n");
00090      return -1;
00091    }
00092    int j;
00093   for( i = 0; i < nb_servers; i++){
00094     for( j = 0; j < nb_tasks; j++){
00095       mapping_matrix[i][j] = 0;
00096     }
00097   }
00098   return 0;
00099 }
00100 
00101 
00102 /*
00103  * This function converts the  binary mapping_matrix to a task to server vector.
00104  * This vector is used to generate a mapping solution.
00105  *
00106  * @param mapping_matrix -- binary matrix which represent task to server assignemnt
00107  * @param nb_servers - number of servers
00108  * @param nb_tasks -- number of tasks
00109  * @param mapping_vector -- task to server assignment vector
00110  *
00111  */
00112 
00113 
00114 int gs_smart_matrix_to_vec(int ** mapping_matrix, int nb_servers, int nb_tasks, int * mapping_vector){
00115   if(!mapping_matrix) return -1;
00116 
00117   int i, j;
00118   for(i=0;i<nb_servers;i++){
00119     for(j=0;j<nb_tasks;j++){
00120       if(mapping_matrix[i][j]==1){
00121         mapping_vector[j]=i;
00122       }
00123     }
00124   }
00125 
00126   return 0;
00127 }
00128 
00129 
00130 #endif