My Project
Loading...
Searching...
No Matches
tmult.cc
Go to the documentation of this file.
1/*******************************************************************
2 * Computer Algebra System SINGULAR
3 *
4 * tmult.cc: p_Mult_nn with pthreads - experimental
5 *
6 *******************************************************************/
7
8
9
10#include "kernel/mod2.h"
11#include "kernel/structs.h"
12#include "kernel/numbers.h"
13#include "kernel/polys.h"
14#ifdef SI_THREADS
15#include <pthread.h>
16#include <stdlib.h>
17#include <stdio.h>
18
19#define NUM_THREADS 8
20#define THREAD_MIN_LENGTH 10*NUM_THREADS
21
22struct p_Mult_nn_thread_data
23{
24 int thread_id;
25 poly p;
26 number n;
27 ring r;
28};
29
30
31void* p_Mult_nn_doMult(void *threadarg)
32{
33 struct p_Mult_nn_thread_data *my_data;
34 my_data = (struct p_Mult_nn_thread_data *) threadarg;
35 //long taskid = my_data->thread_id;
36 poly p = my_data->p;
37 number n = my_data->n;
38 ring r = my_data->r;
39 while (1)
40 {
41 //if (p==NULL) return NULL;
42 if (p==NULL) pthread_exit(NULL);
43 nlInpMult(pGetCoeff(p), n,r);
44 for (int i=0;i<NUM_THREADS;i++)
45 {
46 pIter(p);
47 if (p==NULL) pthread_exit(NULL);
48 //if (p==NULL) return NULL;
49 }
50 }
51 return NULL;
52}
53
54static inline int pLengthOrMore(poly p, int m)
55{
56 int l;
57 for(l=0;(p!=NULL) && (l<=m); l++) pIter(p);
58 return l;
59}
60
61extern "C" poly p_Mult_nn__FieldQ_LengthGeneral_OrdGeneral(poly,const number,const ring);
62
63poly p_Mult_nn_pthread(poly p, const number n, const ring r)
64{
65 if (p==NULL) return NULL;
66 poly q=p;
67 if ((nlSize(n)>2) && (pLengthOrMore(q,THREAD_MIN_LENGTH)>=THREAD_MIN_LENGTH))
68 {
69 pthread_t threads[NUM_THREADS];
70 struct p_Mult_nn_thread_data thread_data_array[NUM_THREADS];
71 pthread_attr_t attr;
72 /* Initialize and set thread detached attribute */
73 pthread_attr_init(&attr);
74 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
75
76 int rc;
77 int t;
78 for(t=0; t<NUM_THREADS; t++)
79 {
80 //printf("In main: creating thread %ld\n", t);
81 thread_data_array[t].thread_id = t;
82 thread_data_array[t].p = p;
83 thread_data_array[t].n = n;
84 thread_data_array[t].r = r;
85 //p_Mult_nn_doMult(&(thread_data_array[t]));
86 rc = pthread_create(&threads[t], &attr, p_Mult_nn_doMult,
87 (void *) &thread_data_array[t]);
88 if (rc)
89 {
90 printf("ERROR; return code from pthread_create() is %d\n", rc);
91 exit(-1);
92 }
93 pIter(p);
94 if (p==NULL) break;
95 }
96 /* Free attribute and wait for the other threads */
97 pthread_attr_destroy(&attr);
98 for(t=NUM_THREADS-1; t>=0; t--)
99 {
100 void *status;
101
102 rc = pthread_join(threads[t], &status);
103 if (rc)
104 {
105 printf("ERROR; return code from pthread_join() is %d\n", rc);
106 exit(-1);
107 }
108 }
109
110 return q;
111 }
112 else
113 {
114 return p_Mult_nn__FieldQ_LengthGeneral_OrdGeneral(p,n,r);
115 }
116}
117#endif
sattr * attr
Definition: attrib.h:16
int l
Definition: cfEzgcd.cc:100
int m
Definition: cfEzgcd.cc:128
int i
Definition: cfEzgcd.cc:132
int p
Definition: cfModGcd.cc:4078
Definition: attrib.h:21
LINLINE void nlInpMult(number &a, number b, const coeffs r)
Definition: longrat.cc:2785
int nlSize(number a, const coeffs)
Definition: longrat.cc:714
#define pIter(p)
Definition: monomials.h:37
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition: monomials.h:44
VAR long thread_id
Definition: shared.cc:230
#define NULL
Definition: omList.c:12
Compatibility layer for legacy polynomial operations (over currRing)
int * status
Definition: si_signals.h:51