source: git/kernel/tmult.cc @ 88479ff

spielwiese
Last change on this file since 88479ff was 896561, checked in by Hans Schoenemann <hannes@…>, 13 years ago
experiment with threads: p_Mult_nn git-svn-id: file:///usr/local/Singular/svn/trunk@14320 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*******************************************************************
2 *  Computer Algebra System SINGULAR
3 *
4 *  tmult.cc: p_Mult_nn with pthreads - experimental
5 *
6 *  Version: $Id$
7 *******************************************************************/
8#include <kernel/mod2.h>
9#include <kernel/structs.h>
10#include <kernel/numbers.h>
11#include <kernel/polys.h>
12#include <kernel/longrat.h>
13#ifdef SI_THREADS
14#include <pthread.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18#define NUM_THREADS     8
19#define THREAD_MIN_LENGTH 10*NUM_THREADS
20
21struct p_Mult_nn_thread_data
22{
23   int  thread_id;
24   poly p;
25   number n;
26   ring r;
27};
28
29
30void*  p_Mult_nn_doMult(void *threadarg)
31{
32  struct p_Mult_nn_thread_data *my_data;
33  my_data = (struct p_Mult_nn_thread_data *) threadarg;
34  //long taskid = my_data->thread_id;
35  poly p = my_data->p;
36  number n = my_data->n;
37  ring r = my_data->r;
38  while (1)
39  {
40    //if (p==NULL) return NULL;
41    if (p==NULL) pthread_exit(NULL);
42    nlInpMult(pGetCoeff(p), n,r);
43    for (int i=0;i<NUM_THREADS;i++)
44    {
45      pIter(p);
46      if (p==NULL) pthread_exit(NULL);
47      //if (p==NULL) return NULL;
48    }
49  }
50  return NULL;
51}
52
53static inline int pLengthOrMore(poly p, int m)
54{
55  int l;
56  for(l=0;(p!=NULL) && (l<=m); l++) pIter(p);
57  return l;
58}
59
60extern "C" poly p_Mult_nn__FieldQ_LengthGeneral_OrdGeneral(poly,const number,const ring);
61
62poly p_Mult_nn_pthread(poly p, const number n, const ring r)
63{
64  if (p==NULL) return NULL;
65  poly q=p;
66  if ((nlSize(n)>2) && (pLengthOrMore(q,THREAD_MIN_LENGTH)>=THREAD_MIN_LENGTH))
67  {
68    pthread_t threads[NUM_THREADS];
69    struct p_Mult_nn_thread_data thread_data_array[NUM_THREADS];
70    pthread_attr_t attr;
71    /* Initialize and set thread detached attribute */
72    pthread_attr_init(&attr);
73    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
74
75    int rc;
76    int t;
77    for(t=0; t<NUM_THREADS; t++)
78    {
79      //printf("In main: creating thread %ld\n", t);
80      thread_data_array[t].thread_id = t;
81      thread_data_array[t].p = p;
82      thread_data_array[t].n = n;
83      thread_data_array[t].r = r;
84      //p_Mult_nn_doMult(&(thread_data_array[t]));
85      rc = pthread_create(&threads[t], &attr, p_Mult_nn_doMult, 
86        (void *) &thread_data_array[t]);
87      if (rc)
88      {
89        printf("ERROR; return code from pthread_create() is %d\n", rc);
90        exit(-1);
91      }
92      pIter(p);
93      if (p==NULL) break;
94    }
95    /* Free attribute and wait for the other threads */
96    pthread_attr_destroy(&attr);
97    for(t=NUM_THREADS-1; t>=0; t--)
98    {
99      void *status;
100
101      rc = pthread_join(threads[t], &status);
102      if (rc)
103      {
104        printf("ERROR; return code from pthread_join() is %d\n", rc);
105        exit(-1);
106      }
107    }
108 
109    return q;
110  }
111  else
112  {
113    return p_Mult_nn__FieldQ_LengthGeneral_OrdGeneral(p,n,r);
114  }
115}
116#endif
Note: See TracBrowser for help on using the repository browser.