gs_seq_data_handle.c

Go to the documentation of this file.
00001 /********************************************************************/
00002 /*                       gs_seq_data_handle.c                       */
00003 /*        the functions for GridSolve data handle processing        */
00004 /*                       Yinan Li, 06/17/2008                       */
00005 /********************************************************************/
00006 
00007 
00008 #include "gs_seq_data_handle.h"
00009 
00010 
00011 /* the global data handle list */
00012 data_handle_list_t *GS_SEQ_DATA_HANDLE_LIST = NULL;
00013 
00014 
00018 int make_new_data_handle_list(GS_DAG_t *dag) {
00019     GS_DAG_Dep_t *dep;
00020     data_handle_t *handle;
00021 
00022     
00023     GS_SEQ_DATA_HANDLE_LIST = 
00024     (data_handle_list_t *) malloc(sizeof(data_handle_list_t));
00025     if (!GS_SEQ_DATA_HANDLE_LIST) {
00026         perror("malloc");
00027         return -1;
00028     }
00029     
00030     GS_SEQ_DATA_HANDLE_LIST->head = NULL;
00031     GS_SEQ_DATA_HANDLE_LIST->tail = NULL;
00032     GS_SEQ_DATA_HANDLE_LIST->num_handles = 0;
00033 
00034     for (dep = dag->head_dep; dep != NULL; dep = dep->next) {
00035 
00036         if (dep->dep_type == INPUT_AFTER_OUTPUT_DEPENDENCY) {
00037 
00038             handle = (data_handle_t *) malloc(sizeof(data_handle_t));
00039             if (!handle) {
00040                 perror("malloc");
00041                 return -1;
00042             }
00043 
00044             handle->producer_func = dep->pnode->func_name;
00045             handle->consumer_func = dep->cnode->func_name;
00046             handle->pseq_id = dep->pnode->handle->problem_desc->seq_id;
00047             handle->cseq_id = dep->cnode->handle->problem_desc->seq_id;
00048             handle->pindex = dep->largp->index;
00049             handle->cindex = dep->rargp->index;
00050             handle->pdptr = dep->largp->data;
00051             handle->cdptr = dep->rargp->data;
00052             handle->used = 0;
00053             handle->visited = 0;
00054 
00055             if (GS_SEQ_DATA_HANDLE_LIST->num_handles == 0) {
00056                 GS_SEQ_DATA_HANDLE_LIST->head = handle;
00057                 GS_SEQ_DATA_HANDLE_LIST->tail = handle;
00058                 handle->prev = NULL;
00059                 handle->next = NULL;
00060                 GS_SEQ_DATA_HANDLE_LIST->num_handles = 1;
00061             } else {
00062                 handle->prev = GS_SEQ_DATA_HANDLE_LIST->tail;
00063                 handle->next = NULL;
00064                 GS_SEQ_DATA_HANDLE_LIST->tail->next = handle;
00065                 GS_SEQ_DATA_HANDLE_LIST->tail = handle;
00066                 GS_SEQ_DATA_HANDLE_LIST->num_handles++;
00067             }
00068         }
00069     }
00070     
00071     printf("# data handles: %d\n", GS_SEQ_DATA_HANDLE_LIST->num_handles);
00072 
00073     return 0;
00074 }
00075 
00076 
00080 int insert_data_handle(char *producer_func, int seq_id, int pindex, 
00081     void * dptr, char *data_handle) {
00082     data_handle_t *handle;
00083 
00084     if (!data_handle) {
00085         fprintf(stderr, "bad data handle\n");
00086         return -1;
00087     }
00088     
00089     if (!GS_SEQ_DATA_HANDLE_LIST) {
00090         fprintf(stderr, "bad data handle list\n");
00091         return -1;
00092     }
00093 
00094     if (GS_SEQ_DATA_HANDLE_LIST->num_handles == 0) {
00095         fprintf(stderr, "no data handle hole found\n");
00096         return -1;
00097     }
00098 
00099     for (handle = GS_SEQ_DATA_HANDLE_LIST->head;
00100         handle != NULL; handle = handle->next) {
00101         if (strcmp(handle->producer_func, producer_func) == 0 &&
00102             /*handle->pseq_id == seq_id && */
00103             handle->pindex == pindex &&
00104             handle->pdptr == dptr &&
00105             !handle->used) {
00106             handle->data_handle = data_handle;
00107             handle->used = 1;
00108         }
00109     }
00110 
00111     return 0;
00112 }
00113 
00114 
00120 char *find_data_handle(char *consumer_func, int seq_id, int cindex, void *dptr) {
00121     data_handle_t *handle;
00122 
00123     if (!GS_SEQ_DATA_HANDLE_LIST) {
00124         fprintf(stderr, "bad data handle list\n");
00125         return NULL;
00126     }
00127         
00128     if (GS_SEQ_DATA_HANDLE_LIST->num_handles == 0) {
00129         fprintf(stderr, "no data handle found\n");
00130         return NULL;
00131     }
00132     
00133     for (handle = GS_SEQ_DATA_HANDLE_LIST->head; 
00134         handle != NULL; handle = handle->next) {
00135         if (strcmp(handle->consumer_func, consumer_func) == 0 &&
00136             /*handle->cseq_id == seq_id && */
00137             handle->cindex == cindex &&
00138             handle->cdptr == dptr &&
00139             handle->used &&
00140             !handle->visited) {
00141             handle->visited = 1;
00142             if (!handle->data_handle) {
00143                 fprintf(stderr, "bad data handle\n");
00144                 return NULL;
00145             } else {
00146                 return handle->data_handle;
00147             }
00148         }
00149     }
00150     
00151     return NULL;
00152 }
00153 
00154 
00158 void free_data_handle_list() {
00159     data_handle_t *handle;
00160 
00161     if (GS_SEQ_DATA_HANDLE_LIST == NULL) return;
00162 
00163     /* there is no data handle to free */
00164     if (GS_SEQ_DATA_HANDLE_LIST->num_handles == 0) {
00165         free(GS_SEQ_DATA_HANDLE_LIST);
00166         GS_SEQ_DATA_HANDLE_LIST = NULL;
00167         return;
00168     }
00169     
00170     /* there is one data handle to free */
00171     if (GS_SEQ_DATA_HANDLE_LIST->num_handles == 1) {
00172         free(GS_SEQ_DATA_HANDLE_LIST->head);
00173         free(GS_SEQ_DATA_HANDLE_LIST);
00174         GS_SEQ_DATA_HANDLE_LIST = NULL;
00175         return;
00176     }
00177 
00178     /* there is more than one data handle to free */
00179     while (GS_SEQ_DATA_HANDLE_LIST->num_handles > 0) {
00180         handle = GS_SEQ_DATA_HANDLE_LIST->tail;
00181         GS_SEQ_DATA_HANDLE_LIST->tail = handle->prev;
00182         free(handle);
00183         GS_SEQ_DATA_HANDLE_LIST->num_handles--;
00184     }
00185 
00186     free(GS_SEQ_DATA_HANDLE_LIST);
00187     GS_SEQ_DATA_HANDLE_LIST = NULL;
00188     
00189     return;
00190 }
00191